code

  • Thread starter Bill Cunningham
  • Start date
B

Ben Bacarisse

Bill Cunningham said:
I mean 1 not zero. I guess zero would be no input.

int c;
c=fscanf...
while(c!=0)...loop

Would that be a correct statement for this or maybe,
while(c!=0||c!=EOF){}

No. That test is always true so it can't ever be what you want in such
a loop. If you can't see why it's always true I fear that programming
may not be for you. (Of course it might just have been at typo -- I
make loads of those.)

I gave you some sounds advice about scanf. Here it is again: all scanf
loops should be written with the test for what you want to get, not for
the negation of what you don't want.

If you want one item you write

while (scanf(...) == 1) ...

If the format is designed to read two items:

while (scanf(...) == 2) ...

Sometimes what you want is more subtle. You may want at least one
number, so you'd write:

while (scanf(...) >= 1) ...

This way you know that you will only process correct data, and that the
loop will stop when anything goes wrong. scanf can fail for all sorts
of reasons and it's fiddly to invert the test correctly, so don't try.
 
B

Bill Cunningham

Ben said:
No. That test is always true so it can't ever be what you want in
such a loop. If you can't see why it's always true I fear that
programming may not be for you. (Of course it might just have been
at typo -- I make loads of those.)

I gave you some sounds advice about scanf. Here it is again: all
scanf loops should be written with the test for what you want to get,
not for the negation of what you don't want.

If you want one item you write

while (scanf(...) == 1) ...

If the format is designed to read two items:

while (scanf(...) == 2) ...

Sometimes what you want is more subtle. You may want at least one
number, so you'd write:

while (scanf(...) >= 1) ...

This way you know that you will only process correct data, and that
the loop will stop when anything goes wrong. scanf can fail for all
sorts of reasons and it's fiddly to invert the test correctly, so
don't try.

Ok so should I use a buffer like the one in the code of 8200 I would try
something like this then?

int c;
c=fscanf...
while(c>=1)...

I'm a little stick here but I'll figure it out. What about the EOF? Ignore
that?

Bill
 
B

Ben Bacarisse

Bill Cunningham said:
Ok so should I use a buffer like the one in the code of 8200 I would try
something like this then?

int c;
c=fscanf...
while(c>=1)...

I'm a little stick here but I'll figure it out. What about the EOF? Ignore
that?

You should not be using scanf at all. It's the wrong tool for this
job. The simplest way to read binary data is with fread.
 
B

Bill Cunningham

Ben said:
You should not be using scanf at all. It's the wrong tool for this
job. The simplest way to read binary data is with fread.

This brings me to another question concerning feof(). I have this code I
will post and Keith told me something I didn't quite get but it has stuck in
my head and I'll address it. I knew this code would work so I have left out
error checking for brevity. Am I using feof() right here or should I be
doing something else?

#include <stdio.h>

int main(void)
{
char buf[8200];
size_t nread, nwrite;
FILE *fp1, *fp2;
fp1 = fopen("6", "rb");
fp2 = fopen("u", "wb");
do {
nread = fread(buf, 1, sizeof(buf), fp1);
nwrite = fwrite(buf, 1, nread, fp2);
}
while (!feof(fp1));
fclose(fp1);
fclose(fp2);
printf("%zu %zu\n", nread, nwrite);
return 0;
}

I used indent to removes the tabs and write in k and r style so this would
be easy to read.

Bill
 
B

Ben Bacarisse

Bill Cunningham said:
This brings me to another question concerning feof(). I have this code I
will post and Keith told me something I didn't quite get but it has stuck in
my head and I'll address it. I knew this code would work so I have left out
error checking for brevity. Am I using feof() right here or should I be
doing something else?

I wouldn't. In a similar style to what I suggested for scanf, I'd loop
based in success not the negation of failure. I'd test loop

...} while (nread > 0 && nwrite > 0);

Anything other that this condition must be a problem of some sort. What
you've done is to test for one reason to stop: the end of the input, but
there are other possible problems: read errors and write errors
especially.
#include <stdio.h>

int main(void)
{
char buf[8200];
size_t nread, nwrite;
FILE *fp1, *fp2;
fp1 = fopen("6", "rb");
fp2 = fopen("u", "wb");
do {
nread = fread(buf, 1, sizeof(buf), fp1);
nwrite = fwrite(buf, 1, nread, fp2);
}
while (!feof(fp1));
fclose(fp1);
fclose(fp2);
printf("%zu %zu\n", nread, nwrite);
return 0;
}

I used indent to removes the tabs and write in k and r style so this would
be easy to read.

It's not hard to read but your use of indent did not fix it.
 
B

Bill Cunningham

Ben said:
I wouldn't. In a similar style to what I suggested for scanf, I'd
loop based in success not the negation of failure. I'd test loop

...} while (nread > 0 && nwrite > 0);

Anything other that this condition must be a problem of some sort.
What you've done is to test for one reason to stop: the end of the
input, but there are other possible problems: read errors and write
errors especially.

So would I check with feof() and ferror(). How is that done? After the
nread>0&&fwrite>0 fails?

Bill
 
B

Ben Bacarisse

Bill Cunningham said:
So would I check with feof() and ferror(). How is that done? After the
nread>0&&fwrite>0 fails?

If that's what you want to do. There may be no reason to check either.
 
K

Keith Thompson

osmium said:
Your man page doesn't say that, now does it?

Why do you assume that? fscanf does return EOF in certain
circumstances.

Here's what "man fscanf" says on my system:

These functions return the number of input items successfully
matched and assigned, which can be fewer than provided for,
or even zero in the event of an early matching failure.

The value EOF is returned if the end of input is reached before
either the first successful conversion or a matching failure
occurs. EOF is also returned if a read error occurs, in which
case the error indicator for the stream (see ferror(3)) is set,
and errno is set indicate the error.
 
B

Barry Schwarz

I wouldn't. In a similar style to what I suggested for scanf, I'd loop
based in success not the negation of failure. I'd test loop

...} while (nread > 0 && nwrite > 0);

I suggest the second test should be nwrite == nread.
 
B

Ben Bacarisse

Barry Schwarz said:
I suggest the second test should be nwrite == nread.

Yes, that's much better (and what I've written in real code more than
once -- I can't have been thinking). Thanks.
 
B

Bill Cunningham

Kenneth said:
To be fair, it says "zero", not "0". However, I would hope you would
know that the two are the same.

It says, flat out:

These functions return the number of input items assigned.

First, you don't say what man page(s) you are showing, so I am going
out on a limb and assuming that it's actually for fscanf(), as well
as the related functions such as scanf() and sscanf(), hence the use
of "these functions".

Yes. fscanf()
Your call to fscanf is:

*buf = fscanf(fp, "%i", buf);

Hence, you are asking for (up to) 1 "item".

Continuing the man page:


Therefore, the only possible non-error returns are 0 and 1.

Had you requested (up to) three items, then 0, 1, 2, and 3 would be
the possible non-error returns. It would be impossible for the man
page (or any other documentation, for that matter) to explicitly list
every possible non-error return. The above two sentences describe
the possible return values in enough detail for any Engligh-speaking
programmer to extrapolate the exact list of possible values for the
exact situation at hand.

OK to be humble I don't call myself a programmer at all. Not by a long
shot. I'm a wannabe. A dream. My approach to the whole fscanf thing I am
told was the wrong way to go.

Bill
 
L

Les Cargill

Bill said:
Yes. fscanf()


OK to be humble I don't call myself a programmer at all. Not by a long
shot. I'm a wannabe. A dream. My approach to the whole fscanf thing I am
told was the wrong way to go.

Bill

It's a lot easier to get a copy of K&R and do the stuff in
there. If you get done early, do it twice :)
 
B

Bill Cunningham

Les said:
It's a lot easier to get a copy of K&R and do the stuff in
there. If you get done early, do it twice :)

What I need as I'm learning is an example. And if I don't understand it
help. I don't know if kandr2 gives examples of everything I might overlook
and coming to clc can be questionable. If I ask for and example the answer
sometimes is "do your own work".

B
 
B

Bill Cunningham

There's also alot the kandr2 doesn't too. Like C99 for one. And
everything that printf can do.

B
 

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

Similar Threads

error 28
write error 13
URGENT 1
Working with files 1
code snippet 92
code 50
strtok 7
fseek 17

Staff online

Members online

Forum statistics

Threads
474,078
Messages
2,570,570
Members
47,204
Latest member
MalorieSte

Latest Threads

Top