A question regarding FAQs question 12.2 
Author Message
 A question regarding FAQs question 12.2

Okay guys, I'm reading the FAQs. I've removed my while(!feof) and replaced
it
with the following. It appears to be working fine. My question is did I do
it right?
I'm beginning to be unsure of anything I do.

        while(fscanf(fp, "%d", &fdata) != EOF)
        {
                printf("Record number %d is %d \n", x, fdata);
                x++;
        }

Thanks in advance Ed Freeberg  



Thu, 24 Aug 2000 03:00:00 GMT  
 A question regarding FAQs question 12.2

: Okay guys, I'm reading the FAQs. I've removed my while(!feof) and replaced
: it
: with the following. It appears to be working fine. My question is did I do
: it right?
: I'm beginning to be unsure of anything I do.

:       while(fscanf(fp, "%d", &fdata) != EOF)
:       {
:               printf("Record number %d is %d \n", x, fdata);
:               x++;
:       }

If fp, fdata, and x have been correctly declared and initialised, then
probably.  Note that fscanf() returns the number of items assigned,
or EOF on failure - it can return 0, meaning no items were assigned,
in which case fdata would not be updated.  The folllowing:
   int j;
   ...
   while (1) {
        j = fscanf(fp, "%d", &fdata);
        if (j == EOF) halt_and_catch_fire();
        else if (j == 1) printf("Record %d is %d\n", x++, fdata);
        else if (j == 0) break;
   }
would give better error checking.  What you do about the various
returns from fscanf() depends on where you're getting your input
from; that's one reason why the scanf() functions are tricky to
use.

Will



Thu, 24 Aug 2000 03:00:00 GMT  
 A question regarding FAQs question 12.2



Quote:
>Okay guys, I'm reading the FAQs. I've removed my while(!feof) and replaced
>it
>with the following. It appears to be working fine. My question is did I do
>it right?
>I'm beginning to be unsure of anything I do.

>        while(fscanf(fp, "%d", &fdata) != EOF)
>        {
>                printf("Record number %d is %d \n", x, fdata);
>                x++;
>        }

This would go into an infinite loop if some non-numeric data was entered.
In that case fscanf() would return 0 as no input itemds were assigned
but there was no error or end-of-file condition on the stream. Depending
on what you want to do in that eventuality you could write:

         while(fscanf(fp, "%d", &fdata) == 1)

...

--
-----------------------------------------


-----------------------------------------



Fri, 25 Aug 2000 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. newbie question about the comp.lang.c FAQs

2. Two newbie questions: CLS Compliance + FAQs

3. Simple Question regarding C# file creation

4. Newbie Question regarding namespaces in VC++

5. Question regarding (old?) CAsyncSocket bug

6. Questions regarding .exe

7. Question in regards to search path

8. Newbie C question regarding static & const variables

9. Question regarding unions and structures

10. Question regarding apartments vs synchronized regions in CLR

11. Preprocessing question regarding macros

12. CPP question regarding macro use

 

 
Powered by phpBB® Forum Software