scanf question

M

mthies

I have two kinds of file names to read in:
"name_a01_002"
"name_a01_opt02_003"
where the numbers will always have the same amount of digits and will
thus always be integers. I am only interested in the first and the last
number and want to set up scanf to give me these values.
I tried something like
int a,b
if( sscanf(filename, "name_a%2d%_*[^_]_%3d", &a, &b) == 2) success=1;
but it works only for the later one.
Is it at all possible to skip the middle part (_opt02) optionally?

Michael
 
A

Alex Fraser

I have two kinds of file names to read in:
"name_a01_002"
"name_a01_opt02_003"
where the numbers will always have the same amount of digits and will
thus always be integers. I am only interested in the first and the last
number and want to set up scanf to give me these values.
I tried something like
int a,b
if( sscanf(filename, "name_a%2d%_*[^_]_%3d", &a, &b) == 2) success=1;
but it works only for the later one.
Is it at all possible to skip the middle part (_opt02) optionally?

There may be some cunning way to do it, but it would probably be simpler to
follow the call above with another that matches the other format (if the
first one did not match).

Alex
 
S

Suman

I have two kinds of file names to read in:
"name_a01_002"
"name_a01_opt02_003"
where the numbers will always have the same amount of digits and will
thus always be integers. I am only interested in the first and the last
number and want to set up scanf to give me these values.
I tried something like
int a,b
if( sscanf(filename, "name_a%2d%_*[^_]_%3d", &a, &b) == 2) success=1;

1. What is the '%' bfore the second underscore doing?
2. The first string (to be parsed) has 2 unbderscores and the second
three.
However, you are always trying to match 3 in your scanf call. Is
that fair?
 
T

tanmoy87544

I have two kinds of file names to read in:
"name_a01_002"
"name_a01_opt02_003"
where the numbers will always have the same amount of digits and will
thus always be integers. I am only interested in the first and the last
number and want to set up scanf to give me these values.
I tried something like
int a,b
if( sscanf(filename, "name_a%2d%_*[^_]_%3d", &a, &b) == 2) success=1;
but it works only for the later one.
Is it at all possible to skip the middle part (_opt02) optionally?

Not with one scanf. But it can be done in one statement:
success = (
(q=p=-1) /* Initialize to sentinels */
&& sscanf(filename,"name_a%2d_%n%*[^_]_%n",&a,&p,&q) /* at
least */
&& p>0 /* Check for underscore after first number */
&& sscanf(&filename[q<0?p:q],"%3d",&b)
));
You can of course make sure that %2d does read 2 characters and not 1
etc.

But, before using something like this, I would ask myself: Is it
obvious that something like this does not unwittingly invoke undefined
behaviour and will that be as obvious when I look at it again? Would I
recognize this construct easily next time I saw it? Is the correctness
obvious enough that I will know it is not the cause of a bug I start
debugging next? Will I be able to change it easily? And, will I stay
alive when the a programmer inherits my code and notices this statement
....
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top