Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > scanf behaviour

Reply
Thread Tools

scanf behaviour

 
 
Mohd Azhar Hussain
Guest
Posts: n/a
 
      03-12-2008
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.
 
Reply With Quote
 
 
 
 
Julienne Walker
Guest
Posts: n/a
 
      03-12-2008
On Mar 12, 8:42 am, Mohd Azhar Hussain <(E-Mail Removed)> wrote:
> 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
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      03-12-2008
Mohd Azhar Hussain wrote:
> 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.

--
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
Falcon Kirtaran
Guest
Posts: n/a
 
      03-12-2008
Mohd Azhar Hussain wrote:
> 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|%[^|]|%[^$]".

--
--Falcon Kirtaran
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      03-12-2008
Mohd Azhar Hussain wrote:
>
> 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.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.



--
Posted via a free Usenet account from http://www.teranews.com

 
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
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
confused about behaviour of scanf Lalatendu Das C Programming 33 01-04-2006 05:27 AM
Scanf Behaviour sajjanharudit@gmail.com C Programming 7 12-30-2005 07:20 PM
Correct behaviour of scanf and sscanf Rob Thorpe C Programming 6 03-15-2005 04:25 PM



Advertisments