argc and argv

Discussion in 'C++' started by userfriendly, Feb 29, 2004.

  1. userfriendly

    userfriendly Guest

    Hi, I know its possible to get argv and argc easily when your commands
    are typed in (cin), but how do you get the values for argv and argc
    when you are reading them from a file, each line of the file being a
    command for running a program. I am trying to do it using getline,
    but it doesnt work correclty. here is some code. thank you.

    while (!infile.eof())
    {
    int i = 1;
    argc = 0;
    infile.getline(process,sizeof(process));

    p1 = strtok(process, " ");
    argv[0] = p1;

    I then run the tokenizer in a loop to fill in the rest of argv, but it
    doesnt work correctly if there is a space in the directory name before
    the args.
    Eg. it will for for something like this:
    c:\temp\notepad.exe /w test.txt

    but not for this:
    c:\temp 2\notepad.exe

    any help is appreciated.
    userfriendly, Feb 29, 2004
    #1
    1. Advertising

  2. "userfriendly" <> wrote in message
    news:...
    > Hi, I know its possible to get argv and argc easily when your commands
    > are typed in (cin), but how do you get the values for argv and argc
    > when you are reading them from a file, each line of the file being a
    > command for running a program. I am trying to do it using getline,
    > but it doesnt work correclty. here is some code. thank you.
    >
    > while (!infile.eof())
    > {
    > int i = 1;
    > argc = 0;
    > infile.getline(process,sizeof(process));
    >
    > p1 = strtok(process, " ");
    > argv[0] = p1;
    >
    > I then run the tokenizer in a loop to fill in the rest of argv, but it
    > doesnt work correctly if there is a space in the directory name before
    > the args.
    > Eg. it will for for something like this:
    > c:\temp\notepad.exe /w test.txt
    >
    > but not for this:
    > c:\temp 2\notepad.exe
    >
    > any help is appreciated.


    Quite apart from the code errors, what you are trying cannot easily work.

    c:\temp 2\notepad.exe

    You say the program is called notepad.exe and is in the "c:\temp 2"
    directory. But it could just as easily be that the program is called temp
    and is in the "c:\" directory. What you have is just ambiguous, there is no
    easy way to tell the difference between a space that separates two argv
    entries and a space that is in a file name.

    Normally this is handled in Windows by using quotes.

    "c:\temp 2\notepad.exe" /w test.txt

    But now the parsing is more complicated and your simple approach using
    strtok doesn't work.

    john
    John Harrison, Feb 29, 2004
    #2
    1. Advertising

  3. (userfriendly) wrote in message news:<>...
    > Hi, I know its possible to get argv and argc easily when your commands
    > are typed in (cin), but how do you get the values for argv and argc
    > when you are reading them from a file, each line of the file being a
    > command for running a program. I am trying to do it using getline,
    > but it doesnt work correclty. here is some code. thank you.
    >
    > while (!infile.eof())
    > {
    > int i = 1;
    > argc = 0;
    > infile.getline(process,sizeof(process));
    >
    > p1 = strtok(process, " ");
    > argv[0] = p1;
    >
    > I then run the tokenizer in a loop to fill in the rest of argv, but it
    > doesnt work correctly if there is a space in the directory name before
    > the args.
    > Eg. it will for for something like this:
    > c:\temp\notepad.exe /w test.txt
    >
    > but not for this:
    > c:\temp 2\notepad.exe
    >
    > any help is appreciated.


    that's not a C++ issue. It's a DOS issue. Dos thinks that there should
    be no file names with spaces... So to solve you problem do

    "C:\temp 2\notepad.exe"

    instead of

    C:\temp 2\notepad.exe

    :)
    Chris Mantoulidis, Feb 29, 2004
    #3
  4. userfriendly

    Buster Guest

    "userfriendly" <> wrote

    > while (!infile.eof())


    That's not how you write an istream input loop. You will process the
    final line twice. See the FAQ for an explanation.

    Regards,
    Buster.
    Buster, Mar 1, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Claudio Varini

    use of argc and argv

    Claudio Varini, Oct 28, 2004, in forum: C++
    Replies:
    3
    Views:
    2,118
    Howard
    Oct 28, 2004
  2. Billy Patton

    How to alter argc and argv

    Billy Patton, May 31, 2005, in forum: C++
    Replies:
    5
    Views:
    850
    red floyd
    Jun 3, 2005
  3. Hal Styli
    Replies:
    14
    Views:
    1,608
    Old Wolf
    Jan 20, 2004
  4. ern
    Replies:
    2
    Views:
    421
    Walter Roberson
    Aug 17, 2006
  5. vicky

    about argc and argv

    vicky, Jun 27, 2007, in forum: C Programming
    Replies:
    11
    Views:
    574
    Richard Heathfield
    Jun 28, 2007
Loading...

Share This Page