scanf and loop

O

Owner

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?
 
B

Ben Bacarisse

Owner said:
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.
 
O

Owner

Owner said:
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)
 
B

Ben Bacarisse

Owner said:
Owner <[email protected]> writes:
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
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 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.
 
M

Mark Bluemel

... 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)
 
O

Owner

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
 
P

Peter 'Shaggy' Haywood

Groovy hepcat Owner was jivin' in comp.lang.c on Tue, 29 Mar 2011 3:13
am. It's a cool scene! Dig it.
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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top