Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > scanf question

Reply
Thread Tools

scanf question

 
 
mthies@ww.uni-erlangen.de
Guest
Posts: n/a
 
      10-07-2005
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

 
Reply With Quote
 
 
 
 
Alex Fraser
Guest
Posts: n/a
 
      10-07-2005
<(E-Mail Removed)-erlangen.de> wrote in message
news:(E-Mail Removed) oups.com...
> 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


 
Reply With Quote
 
 
 
 
Suman
Guest
Posts: n/a
 
      10-07-2005

http://www.velocityreviews.com/forums/(E-Mail Removed)-erlangen.de wrote:
> 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?

> but it works only for the later one.
> Is it at all possible to skip the middle part (_opt02) optionally?


 
Reply With Quote
 
tanmoy87544@gmail.com
Guest
Posts: n/a
 
      10-07-2005

(E-Mail Removed)-erlangen.de wrote:
> 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
....

 
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
a scanf() question Mik0b0 C Programming 8 11-30-2006 11:31 AM
scanf question using %[^$] john C Programming 8 09-04-2006 04:31 AM
difference between scanf("%i") and scanf("%d") ??? perhaps bug inVS2005? =?ISO-8859-1?Q?Martin_J=F8rgensen?= C Programming 18 05-02-2006 10:53 AM
scanf (yes/no) - doesn't work + deprecation errors scanf, fopen etc. =?ISO-8859-1?Q?Martin_J=F8rgensen?= C Programming 185 04-03-2006 02:49 PM
Newbie-question: scanf alternatives? merman C Programming 55 09-24-2004 12:40 PM



Advertisments