@yellowSnow
And if fgets() returns NULL because of an error (and not because of end of file), then what?
My oversight ... my apologies.
Cheers.
@yellowSnow
And if fgets() returns NULL because of an error (and not because of end of file), then what?
My oversight ... my apologies.
Cheers.
Make A C Program That Performs The Following Algorithm:
(a)Use Scan F To Input The Following Data:
-assign Value 26 To Age.
-assign Value 80 To Weight In Kg.
-assign The Value 183.219 To Height Cm.
(b)Calculate The Weight-to-height Ratio.
(c)Display A Heading For The Results (d)Display The Results In A Suitable Format?
I think you better show some effort by attempting to do this yourself - otherwise you will not get any assistance. At least post some code and highlight the areas you're having difficulties with.
Regards,
JD
Don't use feof() to control a loop
Use
while ( fgets(buffer,sizeof(buffer),myfile) != NULL )
Hi Salem,
I completely agree with your code snippet for checking EOF as C input functions return values that can be used to test for this condition.
However, you could still use the feof() function to control a loop if you use a priming read and move the input function call to the bottom of the loop. (Sorry, I know you know this - this is just an information "tidbit" for the OP).
fgets(buf, BUFSIZ, fp);
while(!feof(fp)) {
fputs(buf, stdout);
fgets(buf, BUFSIZ, fp);
}
I wouldn't do it like this in C for the reason I stated previously, but I have used this style in other languages where getting a hold of the return value from the input function was a tad "cumbersome" and coding the loop (especially when calling the input function at the top of the loop) became messy.
Cheers,
JD