PDA

View Full Version : fscanf() and getopt()


felix
04-12-2005, 11:30 PM
Hello,

1 - Guys, about fscanf() I know I can limit the amount of data it will read, in this way for example i limit in 64 bytes

fscanf(fp, "%64s", buff);

I remeber I heard that is possible to pass a parameter to fscanf() that allow it to read strings with spaces, someone know how to do it? And can I use it together with the size delimiter (for example "%64s") ?

2 - I'm testing getopt(), it work cool, I'm using something like that

while ((c = getopt (argc, argv, "w:s:e:l:f:")) != EOF)

switch(c){

case 'x':

....
break;
}

It work fine. To get values from each switched value, but exist a way to create dependencies ?

For example

Suppose I want that:

'l' ONLY can be used if called with 'f'.
's' ONLY can be used if called with 'f'.

But yet, be able to call 'f' single. Can it be done via getopt() ?

ps: Examples are welcome.

3 - Exist a way to know what option of getopt() was choosed. Example, suppose it choosed 'w' and in sequence it entered in 's', can I from 's' case check if 'w' was entered by the user ? something like isgetopt(c); ? hhehehe

Thkz.

Regards again,

mlampkin
04-13-2005, 08:39 AM
For the fscanf and reading embedded WS... try using the c option instead of s... that should do the trick...

Just remember the c option does not add a null at the end of the read in string... so you have to set the input array to null before the call or you won't be able to tell where the string actually ended...


For the getopt(...) call...

There are a lot of extensions which allow you to specify flags which have optional arguments... For example the GNU getopt uses two colons after a flag in the getopt call to signify that an argument is optional...

But you would still have to check the flag argument to see if it was one of the expected letters... and this is an extension so its not standard... and wouldn't work on all systems...

I think your best bet for compatibility is probably to keep it simple... create variables for each expected flag... and as the flags are detected, set them to indicate they occurred... then after getopt signals it is finished, check those flags for discrepancies ( e.g. an "s" flag occurred without an "f" flag )...

There are, of course, other possibilities... these are just suggestions...

I am not certain I understand the last question about getopt... :-/


Michael

RobSeace
04-13-2005, 01:28 PM
Actually, I think maybe you're talking about *scanf()'s bracketed expressions... Eg:
"%64[^\n]" would scan into a buffer (which it will null-terminate) no more than 64
chars, up until it hits a newline...

And, I'm not sure I grasp the getopt() question, either... It sounds like maybe you
want to know about previously encountered opts while processing one opt? If so,
you are supposed to just handle that yourself... Ie: presumably when you encounter
the "w" opt you mention, you'd have set some variable that lets you know it was
encountered, so you can handle it accordingly; so, when you then encounter an "s"
opt, you can just check that variable and see if you've already encountered "w"
previously or not...

felix
05-31-2005, 07:44 PM
Hi,

Thks, it worked perfectilly! ;)

Regards,