scanf and loop

Discussion in 'C Programming' started by Owner, Mar 28, 2011.

  1. Owner

    Owner Guest

    int v[31];
    int dayc = 0; /* day count read */
    int daytotal = 0; /* day total */


    while (scanf("%*s %*s %d%*c", &daytotal) == 1){
    v[dayc++] = daytotal;

    data file contains

    3-13-2011 tue 2234.93
    4-30-2011 wed 4233.43
    5-23-2011 mon 2314.98
    ..
    ..
    ..

    it only read first line from data file and then quits.
    I'm suspecting something out of argument format.
    I added "%*c" for newline character at the end of line.
    Is this becuase in dos, newline character is cr and lf.
    should I put "%*2c"? then it will fix the problem?
     
    Owner, Mar 28, 2011
    #1
    1. Advertising

  2. Owner <> writes:

    > int v[31];
    > int dayc = 0; /* day count read */
    > int daytotal = 0; /* day total */
    >
    >
    > while (scanf("%*s %*s %d%*c", &daytotal) == 1){
    > v[dayc++] = daytotal;
    >
    > data file contains
    >
    > 3-13-2011 tue 2234.93
    > 4-30-2011 wed 4233.43
    > 5-23-2011 mon 2314.98
    > .
    > .
    > .
    >
    > it only read first line from data file and then quits.
    > I'm suspecting something out of argument format.


    I suggest you "play computer". Take your input and follow the
    description (say from a manual page or a C book) of what the scanf call
    does. You will be surprised, I think.

    I suggest this because I think you'll get more from working it out than
    by someone telling you. If that does not work for you, post what you
    think happens and I a sure someone explain where you've gone wrong.

    > I added "%*c" for newline character at the end of line.
    > Is this becuase in dos, newline character is cr and lf.
    > should I put "%*2c"? then it will fix the problem?


    No, reading one character is enough. If the input is a text string (it
    will be by default) line endings a dealt with automatically -- an input
    function will see a single \n character no matter how line endings are
    represented on the host system.

    --
    Ben.
     
    Ben Bacarisse, Mar 28, 2011
    #2
    1. Advertising

  3. Owner

    Owner Guest

    On Mon, 28 Mar 2011 03:48:33 +0100, Ben Bacarisse wrote:

    > Owner <> writes:
    >
    >> int v[31];
    >> int dayc = 0; /* day count read */
    >> int daytotal = 0; /* day total */
    >>
    >>
    >> while (scanf("%*s %*s %d%*c", &daytotal) == 1){
    >> v[dayc++] = daytotal;
    >>
    >> data file contains
    >>
    >> 3-13-2011 tue 2234.93
    >> 4-30-2011 wed 4233.43
    >> 5-23-2011 mon 2314.98
    >> .
    >> .
    >> .
    >>
    >> it only read first line from data file and then quits.
    >> I'm suspecting something out of argument format.

    >
    > I suggest you "play computer". Take your input and follow the
    > description (say from a manual page or a C book) of what the scanf call
    > does. You will be surprised, I think.
    >
    > I suggest this because I think you'll get more from working it out than
    > by someone telling you. If that does not work for you, post what you
    > think happens and I a sure someone explain where you've gone wrong.
    >
    >> I added "%*c" for newline character at the end of line.
    >> Is this becuase in dos, newline character is cr and lf.
    >> should I put "%*2c"? then it will fix the problem?

    >
    > No, reading one character is enough. If the input is a text string (it
    > will be by default) line endings a dealt with automatically -- an input
    > function will see a single \n character no matter how line endings are
    > represented on the host system.


    I tried this, too

    while (scanf("%*s %*s %d[\n]", &daytotal) == 1)
     
    Owner, Mar 28, 2011
    #3
  4. Owner <> writes:

    > On Mon, 28 Mar 2011 03:48:33 +0100, Ben Bacarisse wrote:
    >
    >> Owner <> writes:

    <snip>
    >>> while (scanf("%*s %*s %d%*c", &daytotal) == 1){
    >>> v[dayc++] = daytotal;
    >>>
    >>> data file contains
    >>>
    >>> 3-13-2011 tue 2234.93
    >>> 4-30-2011 wed 4233.43
    >>> 5-23-2011 mon 2314.98

    <snip>
    >> I suggest you "play computer". Take your input and follow the
    >> description (say from a manual page or a C book) of what the scanf call
    >> does. You will be surprised, I think.

    <snip>
    > I tried this, too
    >
    > while (scanf("%*s %*s %d[\n]", &daytotal) == 1)


    Did you mean %[\n]? What you wrote is something else altogether. You
    can keep trying various options in the hope of hitting the correct one
    eventually (the odds are not good) or you can follow through exactly
    what it is that your code is doing, step by step. The specification of
    what a scanf format does are not difficult but they are brutally exact
    and you need to understand them to know what you've asked scanf to do
    and what you should have asked scanf to do.

    If, on the pother hand, you just need to pull some data out of a text
    file and learning the details of scanf is not important to you, then you
    should switch to using a more suitable tool. I know you are using a
    Windows system but presumably there is something better than C for text
    manipulation that could do the job. Awk or Perl come to mind.

    --
    Ben.
     
    Ben Bacarisse, Mar 28, 2011
    #4
  5. Owner

    Mark Bluemel Guest

    On 03/28/2011 02:01 PM, Ben Bacarisse wrote:
    > ... You
    > can keep trying various options in the hope of hitting the correct one
    > eventually (the odds are not good) or you can follow through exactly
    > what it is that your code is doing, step by step.


    Or you can post your attempts up to a newsgroup and hope someone will
    fix them for you, so that you don't fail your assignment...

    "The Gods do not protect fools. Fools are protected by more capable
    fools." (Larry Niven)
     
    Mark Bluemel, Mar 28, 2011
    #5
  6. Owner

    Owner Guest

    On Mon, 28 Mar 2011 14:05:15 +0100, Mark Bluemel wrote:

    > On 03/28/2011 02:01 PM, Ben Bacarisse wrote:
    >> ... You
    >> can keep trying various options in the hope of hitting the correct one
    >> eventually (the odds are not good) or you can follow through exactly
    >> what it is that your code is doing, step by step.

    >
    > Or you can post your attempts up to a newsgroup and hope someone will
    > fix them for you, so that you don't fail your assignment...
    >
    > "The Gods do not protect fools. Fools are protected by more capable
    > fools." (Larry Niven)


    I finally got it

    while (scanf("%*s %*s %d%*s\n", &daytotal) == 1)

    Thank you all for replying
     
    Owner, Mar 28, 2011
    #6
  7. Groovy hepcat Owner was jivin' in comp.lang.c on Tue, 29 Mar 2011 3:13
    am. It's a cool scene! Dig it.

    > On Mon, 28 Mar 2011 14:05:15 +0100, Mark Bluemel wrote:
    >
    >> On 03/28/2011 02:01 PM, Ben Bacarisse wrote:
    >>> ... You
    >>> can keep trying various options in the hope of hitting the correct
    >>> one eventually (the odds are not good) or you can follow through
    >>> exactly what it is that your code is doing, step by step.

    >>
    >> Or you can post your attempts up to a newsgroup and hope someone will
    >> fix them for you, so that you don't fail your assignment...
    >>
    >> "The Gods do not protect fools. Fools are protected by more capable
    >> fools." (Larry Niven)

    >
    > I finally got it
    >
    > while (scanf("%*s %*s %d%*s\n", &daytotal) == 1)


    Yes, but do you actually understand why that works? If not, then
    perhaps you should review your C reference manual. What does the %d
    conversion specifier of scanf() match? And what is the third field of
    your input? So how does %d treat the third field of your input? And so,
    what is left unread at the end of the line after the %d has matched? So
    what, then, is the next scanf() call reading, and what effectively is
    the third field of that?

    --
    Dig the sig!

    ----------- Peter 'Shaggy' Haywood ------------
    Ain't I'm a dawg!!
     
    Peter 'Shaggy' Haywood, Apr 1, 2011
    #7
    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. Guest

    scanf in a for loop

    Guest, Dec 25, 2004, in forum: C Programming
    Replies:
    15
    Views:
    3,876
    Lawrence Kirby
    Dec 29, 2004
  2. =?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,482
    those who know me have no need of my name
    Apr 3, 2006
  3. obdict

    using scanf() in while loop

    obdict, Mar 1, 2006, in forum: C Programming
    Replies:
    6
    Views:
    874
    Pedro Graca
    Mar 2, 2006
  4. =?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:
    700
    Richard Bos
    May 2, 2006
  5. Isaac Won
    Replies:
    9
    Views:
    419
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page