Thread: reading text

  1. #31
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Daved View Post
    So Test4 still isn't quite right because of the null terminator issue, and Test3 and Test5 are correct (assuming you want newline conversion).

    I'm still interested to see if you can get Test4 to give the same size output as Test3 and Test5. How do you tell how many bytes were read by read? You need to set a null terminator at that value every time (or at least the last time, the other times you can set block[BUFSIZ] to null).
    I don't think there's an issue with setting block[SIZBUF] to NUL since it doesn't seem to be overflowing. It's just not adding the last 352 bytes to the string.
    I'll look at it some more later, but if you want to try it sooner, I posted the whole source code for it...

  2. #32
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    OK, I fixed Test4. I need the istream::gcount() function:
    Code:
    void Test4( ifstream&  file )
    {
    	char block[BUFSIZ + 1];
    	string fileData;
    	while ( file.read( block, BUFSIZ ) )
    	{
    		fileData += block;
    	}
    	fileData.append( block, file.gcount() );
    	cout << "Test4 Bytes Read: " << fileData.size() << endl;
    }

  3. #33
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I also fixed Test1() if anyone's still interested:
    Code:
    void Test1( ifstream&  file )
    {
    	string line;
    	string fileData;
    	while ( getline( file, line ) )
    	{
    		fileData += line;
    		if ( file.eof() == false )
    		{
    			fileData += "\n";
    		}
    	}
    	cout << "Test1 Bytes Read: " << fileData.size() << endl;
    }
    Now they all report the same size, except Test2() which is fatally broken, since it not only doesn't extract CRLF's, but it also doesn't extract spaces or any other whitespace.

  4. #34
    Registered User
    Join Date
    Sep 2007
    Posts
    6
    Thanks for all your comments. I am going with test1.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading a char at a time from text
    By dudeomanodude in forum C++ Programming
    Replies: 7
    Last Post: 01-29-2008, 12:27 PM
  2. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  3. reading from a text file help......
    By jodders in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2005, 12:51 PM
  4. Reading text file and structuring it..
    By Killroy in forum C Programming
    Replies: 20
    Last Post: 11-19-2004, 08:36 AM
  5. Reading Tab Separted Text files
    By Cathy in forum C Programming
    Replies: 1
    Last Post: 02-15-2002, 10:28 AM