tags:

views:

666

answers:

3

Hello,

I have a text file that has strings on each line. I want to increment a number for each line in the text file, but when it reaches the end of the file it obviously needs to stop. I've tried doing some research on EOF, but couldn't really understand how to use it properly.

I'm assuming I need a while loop, but I'm not sure how to do it.

Thanks!

A: 

Check feof() function

qrdl
+8  A: 

One possible C loop would be:

#include <stdio.h>
int main()
{
    int c;
    while ((c = getchar()) != EOF)
    {
        /*
        ** Do something with c, such as check against '\n'
        ** and increment a line counter.
        */
    }
}

For now, I would ignore feof and similar functions. Exprience shows that it is far too easy to call it at the wrong time and process something twice in the belief that eof hasn't yet been reached.

Pitfall to avoid: using char for the type of c. getchar returns the next character cast to an unsigned char and then to an int. This means that on most [sane] platforms the value of EOF and valid "char" values in c don't overlap so you won't ever accidentally detect EOF for a 'normal' char.

Charles Bailey
thait will never work you didnt defined eof
streetparade
I `#include` stdio.h.
Charles Bailey
see my answer i defined a eof
streetparade
@streetparade: To what effect?
Charles Bailey
A valid character value from `getchar` can *never* overlap with `EOF` on any conforming platform, because characters returned by `getchar` are in the range of `unsigned int` (therefore non-negative), and `EOF` must be negative.
caf
@caf: That's what I thought until it was pointed out to me that there are platforms with 32-chars and 32-ints. This means that char -> unsigned char -> int can end up negative. Hence '[sane]'.
Charles Bailey
Oh yes, and good advice about ignoring `feof` - in my experience, use of that function almost always indicates a bug. Testing the return value of the most recent input function call is sufficient and correct in 99% of cases.
caf
I would say that on such a platform, the for the `getchar` implementation to be conforming it would have to take pains that unsigned character values greater than `INT_MAX` are either mapped to a value less than `INT_MAX` or ignored completely - mapping them to negative integers would be non-conforming, since it would not be "an integer in the range of `unsigned char`".
caf
+3  A: 

How you detect EOF depends on what you're using to read the stream:

function                  result on EOF or error                    
--------                  ----------------------
fgets()                   NULL
fscanf()                  number of succesful conversions
                            less than expected
fgetc()                   EOF
fread()                   number of elements read
                            less than expected

Check the result of the input call for the appropriate condition above, then call feof() to determine if the result was due to hitting EOF or some other error.

Using fgets():

 char buffer[BUFFER_SIZE];
 while (fgets(buffer, sizeof buffer, stream) != NULL)
 {
   // process buffer
 }
 if (feof(stream))
 {
   // hit end of file
 }
 else
 {
   // some other error interrupted the read
 }

Using fscanf():

char buffer[BUFFER_SIZE];
while (fscanf(stream, "%s", buffer) == 1) // expect 1 successful conversion
{
  // process buffer
}
if (feof(stream)) 
{
  // hit end of file
}
else
{
  // some other error interrupted the read
}

Using fgetc():

int c;
while ((c = fgetc(stream)) != EOF)
{
  // process c
}
if (feof(stream))
{
  // hit end of file
}
else
{
  // some other error interrupted the read
}

Using fread():

char buffer[BUFFER_SIZE];
while (fread(buffer, sizeof buffer, 1, stream) == 1) // expecting 1 
                                                     // element of size
                                                     // BUFFER_SIZE
{
   // process buffer
}
if (feof(stream))
{
  // hit end of file
}
else
{
  // some other error interrupted read
}

Note that the form is the same for all of them: check the result of the read operation; if it failed, then check for EOF. You'll see a lot of examples like:

while(!feof(stream))
{
  fscanf(stream, "%s", buffer);
  ...
}

This form doesn't work the way people think it does, because feof() won't return true until after you've attempted to read past the end of the file. As a result, the loop executes one time too many, which may or may not cause you some grief.

John Bode