scanf behaviour

M

Mohd Azhar Hussain

I am novice C programmer.

i have to read a record from file which looks like
c|string1|string2$c|string1|string2$

I am trying to read using fscanf like
fscanf(fp,"%c|%[^|]s%[^$]s",&ch,string1,string2);

but im not getting c ,string1,string2

after playing around for some time I changed my fscanf to

fscanf(fp,"%c|%[^|]%c[^$]s",&ch,string1,string2);

which read successfully

can anyone tell me what's happening.

Thanks in advance.
 
J

Julienne Walker

I am novice C programmer.

i have to read a record from file which looks like
c|string1|string2$c|string1|string2$

I am trying to read using fscanf like
fscanf(fp,"%c|%[^|]s%[^$]s",&ch,string1,string2);

but im not getting c ,string1,string2

after playing around for some time I changed my fscanf to

fscanf(fp,"%c|%[^|]%c[^$]s",&ch,string1,string2);

which read successfully

can anyone tell me what's happening.

Thanks in advance.

Scansets are a separate conversion specifier, not a part of the "%s"
specifier. In other words "%[^|]s" will read up to a vertical bar
character, then try to read a literal 's'. However, because the
vertical bar isn't extracted, fscanf is always going to compare '|'
with 's', it's always not going to match, and fscanf will always fail
at that point. When you inadvertently changed "%[^|]s" to "%[^|]", the
problem went away.


-Jul
 
E

Eric Sosman

Mohd said:
I am novice C programmer.

i have to read a record from file which looks like
c|string1|string2$c|string1|string2$

I am trying to read using fscanf like
fscanf(fp,"%c|%[^|]s%[^$]s",&ch,string1,string2);

but im not getting c ,string1,string2

after playing around for some time I changed my fscanf to

fscanf(fp,"%c|%[^|]%c[^$]s",&ch,string1,string2);

which read successfully

can anyone tell me what's happening.

Perhaps someone can, but I cannot. The code you have
shown should

- Match the input's initial 'c' with the "%c" specifier
and store the 'c' in the variable ch,

- Match the input's first '|' with the format's literal
'|' character and skip past it,

- Match the input's "string1" with the "%[^|] specifier
and store those seven characters plus a '\0' in the
string1 array,

- Match the input's second '|' with the second "%c"
specifier and store the '|' at the start of the
string2 array, and

- Stop with a matching failure, because the format's
literal '[' does not match the input's 's'.

Either you are mistaken when you say this was successful,
or the code you showed us is not the code you ran.
 
F

Falcon Kirtaran

Mohd said:
I am novice C programmer.

i have to read a record from file which looks like
c|string1|string2$c|string1|string2$

I am trying to read using fscanf like
fscanf(fp,"%c|%[^|]s%[^$]s",&ch,string1,string2);

but im not getting c ,string1,string2

after playing around for some time I changed my fscanf to

fscanf(fp,"%c|%[^|]%c[^$]s",&ch,string1,string2);

which read successfully

can anyone tell me what's happening.

Thanks in advance.

The behaviour of scanf() is a bit different from what you think it is.
If you'd read the documentation more closely (I know it is quite
opaque), you'd notice the following:

- %[^|] is its own format specifier. The trailing specifier you have
added is actually treated as a literal, and so after it finds that the |
is not the character you put after it, it will return (and stop getting
more data for you). The return status of the function indicates this by
saying how many variables were assigned to.

- %[^|] will not read the | into the string, so you might want to put a
| after it unless you want the | to be considered for the next specifier.

- Trailing literal text in a scanf() format string does nothing.

What you should have written is something like "%c|%[^|]|%[^$]".
 
C

CBFalconer

Mohd said:
I am novice C programmer.

i have to read a record from file which looks like
c|string1|string2$c|string1|string2$. I am trying to read using
fscanf like "fscanf(fp,"%c|%[^|]s%[^$]s",&ch,string1,string2);"
but im not getting c ,string1,string2. after playing around for
some time I changed my fscanf to

fscanf(fp,"%c|%[^|]%c[^$]s",&ch,string1,string2);

which read successfully

can anyone tell me what's happening.

You are not checking the fscanf return value. Read the function
description.
 

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,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top