EOF reading last line twice - c

I am pretty new to C and have a very simple function for displaying file contents here. It works fine, except the last line of my file prints twice...I know that it has to do w/EOF but I can't figure out how to get the function to recognize EOF as the last line and not run once more. I know there are a billion places on the internet with similar issues, but lots were for C++ and since I am new I thought it would be best to just use my own code. Here is the code:
{
int count=0, fileEnd=0;
FILE* rockPtr=fopen("rockact.txt", "r");
printf("\n%8s%8s%8s%8s%8s\n", "BANDID", "NAME", "SIZE", "CREW", "TRANS");
do
{
fileEnd=fscanf(rockPtr, "%d%s%d%d%s", &(tempBand.rockid), tempBand.bandname, &(tempBand.bandsize), &(tempBand.crewsize), tempBand.transport);
if (fileEnd !=EOF); //checks EOF has not been reached
{
printf("\n%8d%8s%8d%8d%8s", tempBand.rockid, tempBand.bandname, tempBand.bandsize, tempBand.crewsize, tempBand.transport);
count++;
}
}
while (fileEnd !=EOF);
fclose(rockPtr);
printf("\n The total amount of rock acts on file is %d\n", count);
}

Your if condition doesn't want the semi-colon:
if (fileEnd !=EOF); // This semicolon is wrong!
The semicolon is a null statement and is the body of the if.
I'd rather see the whole loop cast as a while loop:
while (fscanf(rockPtr, "%d%s%d%d%s", &tempBand.rockid, tempBand.bandname,
&tempBand.bandsize, &tempBand.crewsize, tempBand.transport)) == 5)
{
printf("\n%8d%8s%8d%8d%8s", tempBand.rockid, tempBand.bandname,
tempBand.bandsize, tempBand.crewsize, tempBand.transport);
count++;
}
If you want to worry about it, you can spot the difference between EOF, read error and format error after the loop. Note that the check is that all values were converted OK.

you have ; after if - remove it
also, check manual for fscanf
If a reading error happens or the end-of-file is reached while
reading, the proper indicator is set (feof or ferror). And, if either
happens before any data could be successfully read, EOF is returned.
This mean that you can read at least partial data from file, reach EOF or error, but fscanf will not return it.
You should use feof function to check whether end of file is reached
so your logic should be:
read from file
if anything is read - display it, here I mean you should compare returned number with count of arguments, not with EOF
check for feof
UPDATE: during opening/reading from file you should always check ferror, as EOF is not the only problem

Related

fscanf is scanning an extra character at the end

it is always printing an extra character at the end. here is the code:
#include <stdio.h>
int main ()
{
char bit;
FILE *fp_read,*fp_write;
fp_read = fopen("test.txt","r");
if(fp_read==NULL) {
printf("Error!! Unable to open the file!!");
return 1;
}
while(!feof(fp_read)) {
fscanf(fp_read,"%c",&bit);
printf("%c",bit);
}
fclose(fp_read);
return 0;
}
if test.txt contains 010101 it prints 0101011 . if 00110 it prints 001100. if it contains abc it prints abcc . that means it always repeats the last character.
What is the problem ? can anybody explain ?
I am not able to reproduce the error.
Refer to David Bowling's first comment in the original post for a neat explanation.
The cppreference page for feof has a shorter version.
The eof function only reports the stream state as reported by the most recent I/O operation, it does not examine the associated data source. For example, if the most recent I/O was a fgetc, which returned the last byte of a file, feof returns zero. The next fgetc fails and changes the stream state to end-of-file. Only then feof returns non-zero.
In typical usage, input stream processing stops on any error; feof and ferror are then used to distinguish between different error conditions.
This means that the use of feof in the while loop may not be appropriate. The last character from the file may be junk and will be different in different systems.
Try doing this instead.
while(fscanf(fp_read,"%c",&bit) != EOF) {
printf("%c",bit);
}

segmentation fault in file handling

int main()
{
FILE *f;
f=fopen("words.txt","r");
char wd[10];
string word;
printf("Enter a word to be searched\n");
word=GetString();
while (!feof(f))
{
fscanf(f,"%s",wd);
//printf("%s\n",wd);
if(strcmp(word,wd)==0)
{
printf("Yes\n");
break;
}
}
fclose(f);
return 0;
}
If the word to be searched, is present in the file, it is giving the correct answer- YES, otherwise, its giving segmentation fault(core dumped).
Please help!!
Line 6: string word;
There is no string data type in C. And,
Line 8: word = GetString();
You didn't include definition of function. Assuming it returns pointer to a string, but won't work anyway because line 6 will not compile.
Use: char word[10] if your sure word won't be longer than 9 characters.
#gnometorule was correct. You're reading one more time than you need to. If you checked your return value, you would find fscanf probably returned EOF. What you want to do is read the file as long as fscanf returns an expected value. Since you are reading 1 item, it should return 1. If it doesn't, there is a problem. Moreover, feof will only be true if the end of the file was reached. What if there was a read error? You may be trying to read from a file that has its error indicator set, and feof won't detect that.
Review the following code. It should give you an idea of how to handle files.
/* This is used to determine whether an end-of-file error message should be printed or the word was found. */
int cmp_result;
/* Stop reading when the file can't be read or the words are equal. */
do
cmp_result = strcmp(word, wd);
while (fscanf(f, "%s", wd) == 1 && cmp_result != 0);
/* Check for a file read error. */
if (ferror(f))
printf("Error reading file\n");
/* End of file reached before words are equal; feof(f) may be true when cmp_result==0 or not, so checking cmp_result is more reliable in this case. */
else if (cmp_result != 0)
printf("Word '%s' not found\n", word);
/* Words are equal. */
else
printf("Yes\n");
The logic is that if the loop was exited, there was a read error or the word was found. The branching statements after the loop test this.
First, the file's error indicator is checked. If it isn't set, the end of the file was reached, or the word was found.
If the word was found, the end of file indicator may be set or not. But if the word wasn't found, the end of file indicator must be set, else the loop wouldn't have exited since we already ruled out the error indicator in the first if statement. This is why we check the comparison result instead of using feof(f).
If those cases fail, we are left with one possibility: the word was found.
I hope this helps!
Anytime while (!feof(f)) is in your code, it almost means that you are in the wrong place. feof() will only be set after reading the end of the stream. It does not indicate, that the next read will be the end of the stream. See here for more info.
Replace
while (!feof(f))
{
fscanf(f,"%s",wd);
... ...
}
by
while (fscanf(f,"%s",wd))
{
... ...
}

Confusion about file I/O (C)

fwrite(&studentg,sizeof(studentg),1,p);
while(!feof(p))
{
printf("flag");
fread(&studentg,sizeof(studentg),1,p);
printf("%s\t%s\t%s\t%s\t%s\t%s\t\n",studentg.name,studentg.add,studentg.tel,studentg.pc,studentg.qq,studentg.email);
}
Why I put only one object in file,but it output two same line?
And if I put two objects in file,it output one object correct,but another repeated.
I try show feof(p)'s return value,it show me that after fread ,feof(p)'s return value is still 0.Can anyone explain how it happens?
You won't get an end of file until you try to read beyond the file. This means that you have to check eof before the print:
fwrite(&studentg,sizeof(studentg),1,p);
finish = 0;
while(!finish)
{
printf("flag");
fread(&studentg,sizeof(studentg),1,p);
finish = feof(p);
if (!finish)
{
printf("%s\t%s\t%s\t%s\t%s\t%s\t\n",studentg.name,studentg.add,studentg.tel,studentg.pc,studentg.qq,studentg.email);
}
}
or
fwrite(&studentg,sizeof(studentg),1,p);
while(1)
{
printf("flag");
fread(&studentg,sizeof(studentg),1,p);
if (feof(p)) break;
printf("%s\t%s\t%s\t%s\t%s\t%s\t\n",studentg.name,studentg.add,studentg.tel,studentg.pc,studentg.qq,studentg.email);
}
From http://www.cplusplus.com/reference/cstdio/feof/:
"This indicator is generally set by a previous operation on the stream that attempted to read at or past the end-of-file."
This means that end of file is usually detected after an operation.
To fix your code, you may for example replace the condition in while loop with 1 or true and break execution when eof is reached (run feof inside loop).
Use of feof is one of the biggest misconception among beginners in File I/O. Everybody at some point has done the same mistake once or twice.
The way you have used it is Pascal's way but C way is different. The difference is::
Pascal's function returns true if the next read will fail because of end of file.
C's function returns true if the last function failed.
Thats why your code prints the last line twice because after the last line is read in and printed out, feof() will still return 0 (false) and the loop will continue. The next fgets() fails and so the line variable holding the contents of the last line is not changed and is printed out again. After this, feof() will return true (since fgets() failed) and the loop ends.
The correct way to use it is::
while( 1 ) {
fgets(line, sizeof(line), fp);
if ( feof(fp) ) /* check for EOF right after fgets() */
break;
fputs(line, stdout);
}
Still better way::
while( fgets(line, sizeof(line), fp) != NULL )
fputs(line, stdout);
First of all you should include a complete, reproducing, example to what you want to do, not a combined fragment of the code, which is hard to reproduce. Otherwise, note that using fwrite()/fread() on struct contents directly is not portable (see the free online book Porting UNIX Software), and is prone to errors. But you didn't provide enough context for us to understand what went wrong.

How feof() works in C

Does feof() checks for eof for the current position of filepointer or checks for the position next to current filepointer?
Thanks for your help !
Every FILE stream has an internal flag that indicates whether the caller has tried to read past the end of the file already. feof returns that flag. The flag does not indicate whether the current file position is as the end of the file, only whether a previous read has tried to read past the end of the file.
As an example, let's walk through what happens, when reading through a file containing two bytes.
f = fopen(filename, "r"); // file is opened
assert(!feof(f)); // eof flag is not set
c1 = getc(f); // read first byte, one byte remaining
assert(!feof(f)); // eof flag is not set
c2 = getc(f); // read second byte, no bytes remaining
assert(!feof(f)); // eof flag is not set
c3 = getc(f); // try to read past end of the file
assert(feof(f)); // now, eof flag is set
This is why the following is the wrong way to use eof when reading through a file:
f = fopen(filename, "r");
while (!feof(f)) {
c = getc(f);
putchar(c);
}
Because of the way feof works, the end-of-file flag is only set once getc
tries to read past the end of the file. getc will then return EOF, which is
not a character, and the loop construction causes putchar to try to write it
out, resulting in an error or garbage output.
Every C standard library input method returns an indication of success or
failure: getc returns the special value EOF if it tried to read past the
end of the file, or if there was an error while reading. The special value is
the same for end-of-file and error, and this is where the proper way to use
feof comes in: you can use it to distinguish between end-of-file and error
situations.
f = fopen(filename, "r");
c = getc(f);
if (c == EOF) {
if (feof(f))
printf("it was end-of-file\n");
else
printf("it was error\n");
}
There is another internal flag for FILE objects for error situations:
ferror. It is often clearer to test for errors instead of "not end of file".
An idiomatic way to read through a file in C is like this:
f = fopen(filename, "r");
while ((c = getc(f)) != EOF) {
putchar(c);
}
if (ferror(f)) {
perror(filename):
exit(EXIT_FAILURE);
}
fclose(f);
(Some error checking has been elided from examples here, for brevity.)
The feof function is fairly rarely useful.
You can get a better understanding of how feof works, by knowing how it's implemented. Here is a simplified version of how the 7th Edition Unix stdio library implements feof. Modern libraries are very similar, adding code offering thread-safety, increased efficiency, and a cleaner implementation.
extern struct _iobuf {
char *_ptr;
int _cnt;
char *_base;
char _flag;
char _file;
} _iob[_NFILE];
#define _IOEOF 020
#define feof(p) (((p)->_flag&_IOEOF)!=0)
#define getc(p) (--(p)->_cnt>=0? *(p)->_ptr++&0377:_filbuf(p))
int
_filbuf(FILE *iop)
{
iop->_ptr = iop->_base;
iop->_cnt = read(fileno(iop), iop->_ptr, BUFSIZ);
if (iop->_cnt == 0) {
iop->_flag |= _IOEOF;
return(EOF);
}
return(*iop->_ptr++ & 0377);
}
The stdio library maintains with each file a structure containing an internal buffer pointed by _base. The current character in the buffer is pointed by _ptr and the number of characters available is contained in _cnt. The getc macro, which is the base for a lot of higher-level functionality, like scanf, tries to return a character from the buffer. If the buffer is empty, it will call _filbuf to fill it. _filbuf in turn will call read. If read returns 0, which means that no more data is available, _filbuf will set the _IOEOF flag, which feof checks each time you call it to return true.
As you can understand from the above, feof will return true the first time you try to read a character past the end of the file (or a library function tries in your behalf). This has subtle implications on the behavior of various functions. Consider a file containing a single character: the digit 1. After you read that character with getc, feof will return false, because the _IOEOF flag is unset; nobody has yet tried to read past the end of the file. Calling getc again will result in a call to read, the setting of the _IOEOF flag, and this will cause feof to return true. However, after reading the number from the same file using fscanf("%d", &n), feof will immediately return true, because fscanf will have tried to read additional digits of the integer.

Why the fscanf is used 2 times with: while( !feof(File))

why when using fscanf to acquire data from a file, is used 2 times, once before the "!feof(fin)" and later, as shown in the code below:
fscanf(fin, "%s", letta);
while(!feof(fin)){
fscanf(fin, "%s", letta);
}
the word read is not the same?
No the word read would not be the same since you read from the file twice, you would get different data each time.
The condition in the while tests to see if you are at the end of file, in order to do that a read attempt must have been made, which requires the fscanf() before the loop.
Once you are inside the loop you want to continue reading from the file. Presumably there would be more code inside the loop to process the data you are reading.
In the code you have posted, if you encountered the end of file with your first read attempt, you wouldn't enter the (while) loop.
Contrast this with a do-while construct where the test is at the bottom of the loop (ie the read occurs only once, at the "bottom" of the loop). There you will always enter the loop at least once.
There's probably no point in using feof() like this here, yet many people who are (I guess, apologies if I'm mistaken) learning C seem to "want to".
The fact is that fscanf() itself has a return value; if the file ends it will fail to do the requested scanning, and it will tell you so. It will even return the special constant EOF if the file ends. So just loop on the return value of fscanf():
while( fscanf(fin, "%s", letta) == 1 )
{
/* Process, parse, store or do whatever with letta. */
}
Because the EOF flag is only set after a call to fscanf. The condition while(!feof(fin)) doesn't make sense when fscanf hasn't been called yet.
feof doesn't return true until after you try to read past the end of file. Imagine a file like the following:
This is a test$
^
where ^ indicates the current location in the file and $ represents EOF. After the first call to fscanf(fin, "%s", letta);, the stream looks like this:
This is a test$
^
After three iterations of the loop (reading "is", "a", and "test"), you have this:
This is a test$
^
At this point, feof(fin) still returns false, because at this point all you know is that you've reached the end of the string "test". So your loop will execute one more time. Since you're at the end of the file, there's nothing to read, so the contents of letta will not be changed, so it will look like you've read "test" twice. Now feof(fin) will return true.
The moral of the story is that you should not use feof as your loop condition; rather, you should use the result of the read operation itself, like so:
errno = 0;
while (fscanf(fin, "%s", letta) == 1)
{
// process letta
}
if (feof(fin))
{
printf("Reached end of file\n");
}
else
{
perror("Error on read");
}

Resources