scanf behaviour

Discussion in 'C Programming' started by Mohd Azhar Hussain, Mar 12, 2008.

  1. 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.
    Mohd Azhar Hussain, Mar 12, 2008
    #1
    1. Advertising

  2. On Mar 12, 8:42 am, 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.


    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
    Julienne Walker, Mar 12, 2008
    #2
    1. Advertising

  3. Mohd Azhar Hussain

    Eric Sosman Guest

    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.

    --
    Eric Sosman, Mar 12, 2008
    #3
  4. 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
    Falcon Kirtaran, Mar 12, 2008
    #4
  5. Mohd Azhar Hussain

    CBFalconer Guest

    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
    CBFalconer, Mar 12, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rob Thorpe

    Correct behaviour of scanf and sscanf

    Rob Thorpe, Mar 14, 2005, in forum: C Programming
    Replies:
    6
    Views:
    451
    Dan Pop
    Mar 15, 2005
  2. Lalatendu Das

    confused about behaviour of scanf

    Lalatendu Das, Dec 21, 2005, in forum: C Programming
    Replies:
    33
    Views:
    1,009
    Dave Thompson
    Jan 4, 2006
  3. Scanf Behaviour

    , Dec 30, 2005, in forum: C Programming
    Replies:
    7
    Views:
    404
    Keyser Soze
    Dec 30, 2005
  4. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    scanf (yes/no) - doesn't work + deprecation errors scanf, fopen etc.

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Feb 16, 2006, in forum: C Programming
    Replies:
    185
    Views:
    3,389
    those who know me have no need of my name
    Apr 3, 2006
  5. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    difference between scanf("%i") and scanf("%d") ??? perhaps bug inVS2005?

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Apr 26, 2006, in forum: C Programming
    Replies:
    18
    Views:
    674
    Richard Bos
    May 2, 2006
Loading...

Share This Page