[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

scanf and loop

Owner

3/28/2011 2:21:00 AM

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?


6 Answers

Ben Bacarisse

3/28/2011 2:49:00 AM

0

Owner <Owner@Owner-PC.com> 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.

Owner

3/28/2011 10:41:00 AM

0

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

> Owner <Owner@Owner-PC.com> 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)

Ben Bacarisse

3/28/2011 1:01:00 PM

0

Owner <Owner@Owner-PC.com> writes:

> On Mon, 28 Mar 2011 03:48:33 +0100, Ben Bacarisse wrote:
>
>> Owner <Owner@Owner-PC.com> 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.

Mark Bluemel

3/28/2011 1:05:00 PM

0

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)

Owner

3/28/2011 5:13:00 PM

0

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


Peter 'Shaggy' Haywood

4/1/2011 2:03:00 AM

0

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!!