Thread: Reading in a filename from shell

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    You're pretty close as it it.

    First a few ideas.
    1. No need to copy argv since you're not modifying it. You can use it directly, just like you do argc.
    2. I'd just check (argv[i][0] == '-') directly instead of using strchr. This will fail if you have an input file or string with a dash in the middle of it, for one thing.
    3. What happens if you run your program with "myprog -o"? Check that argv[i+1] is valid before copying from it.
    4. Be consistent on whether you're going to use pointers to argv[] for filenames or copy them into your own buffer. You should be using the same method for filename and ofile - pick one or the other since consistency will help later on.

    Right now you handle arguments with dashes with no problem. You need to add an else to your check for cases where you see an argument which doesn't start with a dash. If it's the first one you've seen, copy that argv value into your search string. If it's the second one, copy it into the filename to read from.

    An easy way to keep track of what you've read is to initialize keyword and filename to something invalid. For example, make keyword[0] = 0. Then in your loop check it to see if it's still that invalid value. If so, you know you haven't seen a keyword yet so the argument you're processing must be it. Then next time through you'll do the check again, see that keyword is set to a non-empty string and you'll know to set the filename to the current argument.

    You can also add error checking - if both keyword and filename are set and you read another non-dash argument, you know that something is wrong.

    But as others have mentioned, if this is a real program use getopt.

    Anyway, to pass a 2-d array.

    Code:
    void print_args(int argc, const char **argv)
    {
       for (i = 0; i < argc; i++)
          printf ("%s\n", argv[i]);
    }
    
    int main(int argc, char *argv[])
    {
       print_args(argc, argv);
       return 0;
    }
    No need to pass it by reference since you're not going to modify it.

    You'll want something like this to extract arguments :

    Code:
    void parse_args(int argc, const char **argv, const char **filename)
    {
       for (i = 0; i < argc; i++)
          printf ("%s\n", argv[i]);
    
       *filename = argv[2]; /* replace with a real search in your application */
    }
    
    int main(int argc, char *argv[])
    {
       const char *filename;
       parse_args(argc, argv, &filename);
    
       FILE *fp = fopen(filename, "r");
    
       return 0;
    }
    Last edited by KCfromNC; 10-19-2010 at 11:04 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  2. Help with reading strings and opening files PLEASE
    By green2black in forum C Programming
    Replies: 8
    Last Post: 11-17-2008, 05:46 PM
  3. Pass Filename to another function
    By awesmesk8er in forum C Programming
    Replies: 9
    Last Post: 10-24-2008, 01:43 PM
  4. Reading Filename
    By C_ntua in forum C# Programming
    Replies: 6
    Last Post: 10-13-2008, 09:28 AM
  5. Replies: 2
    Last Post: 01-28-2008, 03:07 AM