While Loop Reading From File Ignores Last line if it contains data 
Author Message
 While Loop Reading From File Ignores Last line if it contains data

I am having some trouble with the following while loop.
<CODE>
while(fgets(buffr, (buffrsize * AVG_LINE_SIZE) - 1, infile) != NULL &&
!feof(infile)) {
    ..........do stuff to buffr
Quote:
}

</CODE>
If the last line of the file contains data the body of the loop does not
execute, even though the fgets call works and fills buffr the feof macro
causes the loop to exit.  Now removing the feof and placing it by the
closing brace at the end of the loop doesnt change anything.

If I put a '\n' at the end of the final line containing data so the last
line of the file is blank it works and I get all the data from the
file.  However I want to be able to accept all the data fom both cases
of file.

Mark Carey



Sun, 19 Oct 2003 04:54:33 GMT  
 While Loop Reading From File Ignores Last line if it contains data

Quote:

> I am having some trouble with the following while loop.
> <CODE>
> while(fgets(buffr, (buffrsize * AVG_LINE_SIZE) - 1, infile) != NULL &&
> !feof(infile)) {
>     ..........do stuff to buffr
> }
> </CODE>

I'm not surprised.  Remove the !feof() condition.  And the second
argument to fgets() looks suspicious, too: is buffr declared as
        char buffr[(buffrsize * AVG_LINE_SIZE) - 1];
or is there something funny going on?

--
"The way I see it, an intelligent person who disagrees with me is
 probably the most important person I'll interact with on any given
 day."
--Billy Chambless



Sun, 19 Oct 2003 05:25:22 GMT  
 While Loop Reading From File Ignores Last line if it contains data
Thanks it did work.
I had already tried this but something else funny must have been happening.

Quote:


> > I am having some trouble with the following while loop.
> > <CODE>
> > while(fgets(buffr, (buffrsize * AVG_LINE_SIZE) - 1, infile) != NULL &&
> > !feof(infile)) {
> >     ..........do stuff to buffr
> > }
> > </CODE>

> I'm not surprised.  Remove the !feof() condition.  And the second
> argument to fgets() looks suspicious, too: is buffr declared as
>         char buffr[(buffrsize * AVG_LINE_SIZE) - 1];
> or is there something funny going on?

Here is how buffr is allocated!
<CODE>
 buffr = malloc(buffrsize * AVG_LINE_SIZE * sizeof(char*));
 if(buffr == NULL)
 {
  /* handle malloc error */
  printf("Error: Unable to allocate memory for incoming buffer\n");
  exit(1);
 }
</CODE>
Quote:

> --
> "The way I see it, an intelligent person who disagrees with me is
>  probably the most important person I'll interact with on any given
>  day."
> --Billy Chambless



Sun, 19 Oct 2003 07:28:53 GMT  
 While Loop Reading From File Ignores Last line if it contains data

Quote:



> > > I am having some trouble with the following while loop.
> > > <CODE>
> > > while(fgets(buffr, (buffrsize * AVG_LINE_SIZE) - 1, infile) != NULL &&
> > > !feof(infile)) {
> > >     ..........do stuff to buffr
> > > }
> > > </CODE>

> > I'm not surprised.  Remove the !feof() condition.  And the second
> > argument to fgets() looks suspicious, too: is buffr declared as
> >         char buffr[(buffrsize * AVG_LINE_SIZE) - 1];
> > or is there something funny going on?

> Here is how buffr is allocated!
> <CODE>
>  buffr = malloc(buffrsize * AVG_LINE_SIZE * sizeof(char*));

So you're trying to pass a char ** to fgets when it expects a
char *?  Not a good idea either.  How is buffr declared?
--
Just another C hacker.


Mon, 20 Oct 2003 00:23:55 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Reading last two lines from a file

2. Reading last two lines in a file

3. Last line Read From File...

4. How to omit some lines when reading a data file

5. How to read the last 500 lines?

6. deleting the last line of a text file

7. Reading from a text file line by line

8. Is there a way to read line by line of a file in C

9. is there a way to read line by line of a file in C

10. is there a way to read line by line of a file

11. Getting last line of a file using fstream

12. is there a way to read line by line in a file

 

 
Powered by phpBB® Forum Software