Find the answer to your Linux question:
Results 1 to 2 of 2
I have created my first somewhat significant C program. Unfortunately, sometimes it hangs after about a day and half of execution (on a cluster). I want to create a log ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jul 2010
    Posts
    25

    Access complete, variable length argument list as a string (C/Linux)


    I have created my first somewhat significant C program. Unfortunately, sometimes it hangs after about a day and half of execution (on a cluster). I want to create a log file for each simulation. To keep filenames from different simulations from overwriting each other, I want to give them filenames using the argument passed and the date. How can I get the argv parameter as a string?

    I have int main(int argc, char *argv[]) {...}. I understand that argv is a pointer to characters, and strings are just arrays of characters. But these didn't work:
    Code:
      char logFn[100] = "";
      strcpy(logFn, argv);
    Code:
      char logFn[100] = "";
      strcpy(logFn, *argv);
    So how would you assign all of argv to a string?

  2. #2
    Just Joined!
    Join Date
    Jul 2010
    Posts
    25
    It turns out writing a loop was faster than figuring this out and didn't include the unwanted 0th element
    Code:
      for (i=1; i<argc; i++) {
        strcat(logFn, argv[i]);
        strcat(logFn, "_");
    }
    But for my own edification, I'd like to see any useful answers to the OP.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •