string parsing

P

parjit

I have written a simple utility which will take the command line
string and parse it for command line parameters.

It's almost completely working. What I have is about 3 basic
parameters, then one that takes 2 sub input characters, and finally,
another to allow a filename or such.

This is just for practice.

For example, typing filename.exe -a -b

Will just print "letter a" and "letter b". I also have another option
for -n1 and -n2 to print something like "node 1" and "node 2".

Anyway, all that is peachy.

My problem is that I want another option to take a parameter like:

-f filename.txt

And print that back to the screen. For example:

filename.exe -a -b -n2 -f anything.txt

Should print:

letter a
letter b
node 2
anything.txt

But it doesn't. The anything.txt won't work. Basically, what I did in
my source is have an if statement to read argv[0] for a / or -,
then a switch and case statement to read the letters. No problem.
Later, I have:

case 'f' : if(argv[2] == ' ')

So if the character after "f" is a space, it will know the filename is
the next variable. Here's what I have:

argv++;
printf("%s", argv);

It compiles fine. But if I type:

filename.exe -f anything.txt

It will not display it "anything.txt". I have no idea whats wrong.

I'm not looking for anything to write this for me, since I prefer to
program it out on my own. Can anybody give me some suggestions?
 
S

Seebs

case 'f' : if(argv[2] == ' ')

So if the character after "f" is a space, it will know the filename is
the next variable. Here's what I have:

Why would argv[2] be a space? argv is "-f". It doesn't have a
space. The thing that split your command line up into argv[] components
already ate all the spaces. So what you probably want to check is
whether argv[2] is '\0' -- which it usually should be, unless you
want to have
-fanything.txt
do the same thing as
-f anything.txt

-s
 
Y

Yoshi

I have written a simple utility which will take the command line
string and parse it for command line parameters.

It's almost completely working. What I have is about 3 basic
parameters, then one that takes 2 sub input characters, and finally,
another to allow a filename or such.

This is just for practice.

For example, typing filename.exe -a -b

Will just print "letter a" and "letter b". I also have another option
for -n1 and -n2 to print something like "node 1" and "node 2".

Anyway, all that is peachy.

My problem is that I want another option to take a parameter like:

-f filename.txt

And print that back to the screen. For example:

filename.exe -a -b -n2 -f anything.txt

Should print:

letter a
letter b
node 2
anything.txt

But it doesn't. The anything.txt won't work. Basically, what I did in
my source is have an if statement to read argv[0] for a / or -,
then a switch and case statement to read the letters. No problem.
Later, I have:

case 'f' : if(argv[2] == ' ')

So if the character after "f" is a space, it will know the filename is
the next variable. Here's what I have:

argv++;
printf("%s", argv);

It compiles fine. But if I type:

filename.exe -f anything.txt

It will not display it "anything.txt". I have no idea whats wrong.

I'm not looking for anything to write this for me, since I prefer to
program it out on my own. Can anybody give me some suggestions?


Hello, here is my try:
I also recommend using getopt if it is available.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
int i=1;
while(i<argc){
switch ( argv[0] )
{
case '-':
switch ( argv[1] )
{
case 'n':
if(argv[2]){
printf("node%s\n", &argv[2]);
}else{
printf("ERROR: specify node number.\n");
exit(-1);
}
break;
case 'f':
if(argv[2]){
printf("file:%s\n", &argv[2]);
}else{
if((i+1)<argc){
printf("file:%s\n", argv[++i]);
}else{
printf("no filename\n");
exit(-1);
}
}
break;
default:
printf("%s\n", &argv[1]);
}
break;
default:
printf("Invalid option:%s\n", &argv[0]);
exit(-1);
}
i++;
}
return 0;
}
 
S

Shao Miller

parjit said:
... ... ...
Later, I have:

case 'f' : if(argv[2] == ' ')

So if the character after "f" is a space, it will know the filename is
the next variable. Here's what I have:

argv++;
printf("%s", argv);

It compiles fine. ... ... ...
I have no idea whats wrong.

I'm not looking for anything to write this for me, since I prefer to
program it out on my own. Can anybody give me some suggestions?


Instead of checking for a space, you could check that the argument count
indicates another string after the '-f' option. You can then assume
that the next string is the filename.

If you use the frame of mind that you cannot check for spaces, consider
how else you, a human, could determine whether:

./foo -f -z filename

should treat '-z' as the filename or not. Do you make filenames
beginning with a '-' invalid? Do you use '-z' as the filename even if
it should be an option? These are design decisions you have the freedom
to choose.
 
G

Gregory Pietsch

I have written a simple utility which will take the command line
string and parse it for command line parameters.

It's almost completely working. What I have is about 3 basic
parameters, then one that takes 2 sub input characters, and finally,
another to allow a filename or such.

This is just for practice.

For example, typing filename.exe -a -b

Will just print "letter a" and "letter b". I also have another option
for -n1 and -n2 to print something like "node 1" and "node 2".

Anyway, all that is peachy.

My problem is that I want another option to take a parameter like:

-f filename.txt

And print that back to the screen. For example:

filename.exe -a -b -n2 -f anything.txt

Should print:

letter a
letter b
node 2
anything.txt

But it doesn't. The anything.txt won't work. Basically, what I did in
my source is have an if statement to read argv[0] for a / or -,
then a switch and case statement to read the letters. No problem.
Later, I have:

case 'f' : if(argv[2] == ' ')

So if the character after "f" is a space, it will know the filename is
the next variable. Here's what I have:

argv++;
printf("%s", argv);

It compiles fine. But if I type:

filename.exe -f anything.txt

It will not display it "anything.txt". I have no idea whats wrong.

I'm not looking for anything to write this for me, since I prefer to
program it out on my own. Can anybody give me some suggestions?


Look through the newlib source code for the implementation of getopt
and friends.

Gregory Pietsch
 
J

James

Vincenzo Mercuri said:
no. wait4() the child's pid_t and then fflush().

Humm... I do actually have to admit that I am "glad?", that my mother did
not decide to abort me!

:^)
 
L

Lew Pitcher

I have written a simple utility which will take the command line
string and parse it for command line parameters. [snip]
My problem is that I want another option to take a parameter like:

-f filename.txt [snip]
Basically, what I did in
my source is have an if statement to read argv[0] for a / or -,
then a switch and case statement to read the letters. No problem.
Later, I have:

case 'f' : if(argv[2] == ' ')

So if the character after "f" is a space, it will know the filename is
the next variable. Here's what I have: [snip]
I have no idea whats wrong. [snip]
Can anybody give me some suggestions?


If you think about this a bit, you'll see what you are doing wrong. Since
you haven't supplied all your code (and the fragment that you have supplied
is too small to let us suggest /real/ code-wise corrections, I'll address
your problem "in the abstract".

You have a program that takes arguments. Something outside of the program
itself breaks a string (given to your command interpreter) into arguments,
and hands these off to your program, through pointers in the *argv[] array.

That thing that breaks the string into arguments does so at points in the
string that match it's critera for argument separators; specifically, that
thing breaks the string into arguments *at the space character*.

You give that thing the string
"filename.exe -a -b"
and that thing breaks the string into three parts:
"filename.exe"
"-a"
"-b"
and passes pointers to each of these three parts as argv[0], argv[1], and
argv[2]. The " " (space) character that separated each of these parts in
the original string is /gone/. You don't have a string of space-separated
text values any more, you have a list of just the text values only.

With me so far?

So, now you introduce a couple of new textual values into that original
string, and it now looks like
"filename.exe -a -b -f filename.txt"

What do you think that the thing that breaks this string into arguments is
going to do with /this/ string?

You seem to expect it to give you
argv[0] -> "filename.exe"
argv[1] -> "-a"
argv[2] -> "-b"
argv[3] -> "-f filename.txt"

But, why should it? After all, that thing that breaks the command string
into arguments *breaks it apart at the space character*. Unless you have
some way to tell it different, that thing will break the command string
into

argv[0] -> "filename.exe"
argv[1] -> "-a"
argv[2] -> "-b"
argv[3] -> "-f"
argv[4] -> "filename.txt"

So, now you know that if
strcmp(argv,"-f") == 0
then
argv[i+1] points to the filename.

HTH
--
Lew Pitcher
Master Codewright & JOAT-in-training | Registered Linux User #112576
---------- Slackware - Because I know what I'm doing. ------
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,114
Members
46,702
Latest member
VernitaGow

Latest Threads

Top