Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #7805 > unrolled thread

Scanf Problem

Started byLJ <djtheshowstopper@gmail.com>
First post2011-07-06 21:19 -0700
Last post2011-07-10 15:32 -0400
Articles 6 — 5 participants

Back to article view | Back to comp.lang.c


Contents

  Scanf Problem LJ <djtheshowstopper@gmail.com> - 2011-07-06 21:19 -0700
    Re: Scanf Problem John Gordon <gordon@panix.com> - 2011-07-07 04:29 +0000
      Re: Scanf Problem LJ <djtheshowstopper@gmail.com> - 2011-07-06 23:40 -0700
        Re: Scanf Problem Ike Naar <ike@sverige.freeshell.org> - 2011-07-07 07:28 +0000
        Re: Scanf Problem Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-07-07 02:00 -0700
    Re: Scanf Problem lawrence.jones@siemens.com - 2011-07-10 15:32 -0400

#7805 — Scanf Problem

FromLJ <djtheshowstopper@gmail.com>
Date2011-07-06 21:19 -0700
SubjectScanf Problem
Message-ID<eab46c07-89fa-43e1-a2ec-b44b69fcb80b@x41g2000yqd.googlegroups.com>
K&R says that the scanf format string may contain ordinary characters,
which are expected to match the next non-white space character of the
input stream.

However when I am reading the date given as 25           /
12      /         1988  using scanf( "%d/%d/%d", &day, &month,
&year ); it is only reading the day. Why is it not reading the month
and year??

[toc] | [next] | [standalone]


#7806

FromJohn Gordon <gordon@panix.com>
Date2011-07-07 04:29 +0000
Message-ID<iv3cnl$b0p$1@reader1.panix.com>
In reply to#7805
In <eab46c07-89fa-43e1-a2ec-b44b69fcb80b@x41g2000yqd.googlegroups.com> LJ <djtheshowstopper@gmail.com> writes:

> K&R says that the scanf format string may contain ordinary characters,
> which are expected to match the next non-white space character of the
> input stream.

> However when I am reading the date given as 25           /
> 12      /         1988  using scanf( "%d/%d/%d", &day, &month,
> &year ); it is only reading the day. Why is it not reading the month
> and year??

Probably because your input contains spaces which the format string wasn't
told to look for.

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon@panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"

[toc] | [prev] | [next] | [standalone]


#7811

FromLJ <djtheshowstopper@gmail.com>
Date2011-07-06 23:40 -0700
Message-ID<77b19eb4-22ee-47d8-8760-1ebd1c5c4622@d22g2000yqn.googlegroups.com>
In reply to#7806
On Jul 7, 9:29 am, John Gordon <gor...@panix.com> wrote:
> In <eab46c07-89fa-43e1-a2ec-b44b69fcb...@x41g2000yqd.googlegroups.com> LJ <djtheshowstop...@gmail.com> writes:
>
> > K&R says that the scanf format string may contain ordinary characters,
> > which are expected to match the next non-white space character of the
> > input stream.
> > However when I am reading the date given as 25           /
> > 12      /         1988  using scanf( "%d/%d/%d", &day, &month,
> > &year ); it is only reading the day. Why is it not reading the month
> > and year??
>
> Probably because your input contains spaces which the format string wasn't
> told to look for.
>
> --
> John Gordon                   A is for Amy, who fell down the stairs
> gor...@panix.com              B is for Basil, assaulted by bears
>                                 -- Edward Gorey, "The Gashlycrumb Tinies"
That doesn't answer my question, '/' should match the next non-
whitespace character and in input that character is '/' only, so scanf
shud read all the three dates

[toc] | [prev] | [next] | [standalone]


#7819

FromIke Naar <ike@sverige.freeshell.org>
Date2011-07-07 07:28 +0000
Message-ID<slrn3vfsj1ao0k.g6i.ike@sverige.freeshell.org>
In reply to#7811
On 2011-07-07, LJ <djtheshowstopper@gmail.com> wrote:
> On Jul 7, 9:29?am, John Gordon <gor...@panix.com> wrote:
>> In <eab46c07-89fa-43e1-a2ec-b44b69fcb...@x41g2000yqd.googlegroups.com> LJ <djtheshowstop...@gmail.com> writes:
>>
>> > K&R says that the scanf format string may contain ordinary characters,
>> > which are expected to match the next non-white space character of the
>> > input stream.
>> > However when I am reading the date given as 25 ? ? ? ? ? /
>> > 12 ? ? ?/ ? ? ? ? 1988 ?using scanf( "%d/%d/%d", &day, &month,
>> > &year ); it is only reading the day. Why is it not reading the month
>> > and year??
>>
>> Probably because your input contains spaces which the format string wasn't
>> told to look for.
>
> That doesn't answer my question, '/' should match the next non-
> whitespace character and in input that character is '/' only, so scanf
> shud read all the three dates

It does answer your question; you should tell scanf to skip
the whitespace.
Read your scanf documentation. Hint: "d% / %d / %d".

[toc] | [prev] | [next] | [standalone]


#7821

FromMalcolm McLean <malcolm.mclean5@btinternet.com>
Date2011-07-07 02:00 -0700
Message-ID<16d85954-f408-48f5-8921-486656654d0b@v7g2000vbk.googlegroups.com>
In reply to#7811
On Jul 7, 9:40 am, LJ <djtheshowstop...@gmail.com> wrote:
>
> That doesn't answer my question, '/' should match the next non-
> whitespace character and in input that character is '/' only, so scanf
> shud read all the three dates
>
The documentation you read is a bit confusing.

Whitespace matches whitespace, all other characters match themselves,
except for format fields introduced by %, and the % character itself.
--
Lots of programming goodies on my website
http://www.malcolmmclean.site11.com/www

[toc] | [prev] | [next] | [standalone]


#8103

Fromlawrence.jones@siemens.com
Date2011-07-10 15:32 -0400
Message-ID<kabpe8-ca1.ln1@jones.homeip.net>
In reply to#7805
LJ <djtheshowstopper@gmail.com> wrote:
> K&R says that the scanf format string may contain ordinary characters,
> which are expected to match the next non-white space character of the
> input stream.
> 
> However when I am reading the date given as 25           /
> 12      /         1988  using scanf( "%d/%d/%d", &day, &month,
> &year ); it is only reading the day. Why is it not reading the month
> and year??

Because the paraphrase above is not correct: ordinary characters are
expected to match the next characters from the stream, they do not skip
leading whitespace.
-- 
Larry Jones

It COULD'VE happened by accident! -- Calvin

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web