Reading a file...

M

Micah Cowan

Ben Bacarisse said:
This looks a little odd although it is fine. There is no reason to assign
to read the value of the test (fscanf(...) != EOF) since you use read only
on the next line and you can't get to the next line unless read is 1.

while (fscanf(fp, "%lf", &number) != EOF) {

does the same job.

It seems very likely that EdUarDo wanted:

while((read = fscanf(fp, "%lf", &number)) != EOF) {

In which case, he'll still want to test read on the next line.
 
B

Ben Bacarisse

It seems very likely that EdUarDo wanted:

while((read = fscanf(fp, "%lf", &number)) != EOF) {

In which case, he'll still want to test read on the next line.

I agree, and I thought that was the original intent also, but since the
OP's code took no action when 0 is returned (the only other possible value
in this case) I concluded that they were content to assume the input
contains no "matching errors" and I consequently simplified what I had to
write by only offering an equivalent piece of code.

A better answer might have been to add the parentheses and suggest an
action to take when read == 0, or to suggest

while (fscanf(fp, "%lf", &number) == 1)

which at least terminates on all possible inputs! Yes, I should at least
have suggested 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

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,501
Latest member
Ledmyplace

Latest Threads

Top