Detecting EOF in stream

This is a discussion on Detecting EOF in stream within the C++ Programming forums, part of the General Programming Boards category; Hi. My problem is that i am trying to read in data in blocks. but i want to make sure ...

  1. #1
    Wen Resu
    Join Date
    May 2003
    Posts
    219

    Detecting EOF in stream

    Hi. My problem is that i am trying to read in data in blocks. but i want to make sure i'm not reading past end of the files. so i am trying to detect eof
    my code started as this
    Code:
    int ReadToBuf(char *buffer, int Amount, fstream &Stream)
    {
    	int AmountRead = 0;
    	char CharBuf;
    	if (Stream.is_open())
    	
    	{
    		cout<<"File is open\n";
    	
    		while ( (AmountRead < Amount - 1) )
    		   
    		{
    		   
    		   
    		   	Stream.get(CharBuf);// (CharBuf, 1);
    			buffer[AmountRead] = CharBuf;
    			AmountRead++;	
    			
    	   	   
    		 
    	   }
    		
    			return AmountRead;
    	
    	}
    	return -1;
    }

    Then in an attemp to detect EOF i did
    Code:
    int ReadToBuf(char *buffer, int Amount, fstream &Stream)
    {
    	int AmountRead = 0;
    	char CharBuf;
    	if (Stream.is_open())
    	
    	{
    		cout<<"File is open\n";
    	
    		while ( (AmountRead < Amount - 1) && (  !(Stream.rdstate() & ios_base::eofbit) != 0) )
    		   
    		{
    		   
    		   
    		   	Stream.get(CharBuf);// (CharBuf, 1);
    			buffer[AmountRead] = CharBuf;
    			AmountRead++;	
    			
    	   	   
    		 
    	   }
    		
    			return AmountRead;
    	
    	}
    	return -1;
    }
    In my attemp to fix, it can no longer read data, so i'm wondering how to detect that i have reached EOF. i've looked over fstream's class declaration and its ancestors, the function eof() didn't work either.

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    one way to do it (though not suggested) is to do file.peek() and test if it's -1, that will mean you didn't read any data. This isn't the best way to do it though, as it makes it test twice each loop in the buffer, but i've found that it works.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try either:
    Code:
    int ReadToBuf(char *buffer, int Amount, fstream &Stream)
    {
    	int AmountRead = 0;
    	char CharBuf;
    	if (Stream.is_open())
    	
    	{
    		cout<<"File is open\n";
    	
    		while ( (AmountRead < Amount - 1) && Stream.get(CharBuf) )   
    		{
    		   
    		   
    			buffer[AmountRead] = CharBuf;
    			AmountRead++;	
    	   	   
    		 
    		}
    		
    		return AmountRead;
    	
    	}
    	return -1;
    }
    Or this:
    Code:
    int ReadToBuf(char *buffer, int Amount, fstream &Stream)
    {
    	int AmountRead = 0;
    	char CharBuf;
    	if (Stream.is_open())
    	
    	{
    		cout<<"File is open\n";
    	
    		while ( (AmountRead < Amount - 1) && !Stream.eof() )		   
    		{
    		   
    		   
    		   	Stream.get(CharBuf);// (CharBuf, 1);
    			if (!Stream.eof())
    			{
    			   buffer[AmountRead] = CharBuf;
    			   AmountRead++;	
    			}
    	   	   
    		 
    		}
    		
    		return AmountRead;
    	
    	}
    	return -1;
    }

  4. #4
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    -original post deleted-

    oh yah, nevermind

  5. #5
    Wen Resu
    Join Date
    May 2003
    Posts
    219
    Thanks for the replies. gettign back at code now so i gona try your suggestions.
    Last edited by Iamien; 03-01-2004 at 01:36 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Detecting EOF in Named Pipes
    By Junktyz in forum Windows Programming
    Replies: 2
    Last Post: 05-19-2009, 03:44 AM
  2. EOF messing up my input stream?
    By Decrypt in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2005, 04:00 PM
  3. Buffering??
    By Brain Cell in forum C Programming
    Replies: 15
    Last Post: 09-21-2004, 07:57 AM
  4. files won't stop being read!!!
    By jverkoey in forum C++ Programming
    Replies: 15
    Last Post: 04-10-2003, 06:28 AM
  5. eof member function on the stream cin
    By stimpyzu in forum C++ Programming
    Replies: 3
    Last Post: 10-30-2002, 05:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21