Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > argc and argv

Reply
Thread Tools

argc and argv

 
 
userfriendly
Guest
Posts: n/a
 
      02-29-2004
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.
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      02-29-2004

"userfriendly" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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


 
Reply With Quote
 
 
 
 
Chris Mantoulidis
Guest
Posts: n/a
 
      02-29-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (userfriendly) wrote in message news:<(E-Mail Removed). com>...
> 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


 
Reply With Quote
 
Buster
Guest
Posts: n/a
 
      03-01-2004
"userfriendly" <(E-Mail Removed)> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
about argc and argv vicky C Programming 11 06-28-2007 08:07 AM
Command line parameters - argc and argv[] ern C Programming 2 08-17-2006 04:13 PM
How to alter argc and argv Billy Patton C++ 5 06-03-2005 04:09 PM
use of argc and argv Claudio Varini C++ 3 10-28-2004 02:52 PM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM



Advertisments