Skip to main content
gamedev.net
🔒 Locked

simple I/O question (C++)

Started by Dark Rain Feb 21, 2003 at 12:04 AM 1 replies 200+ views
Original Post
Dark Rain
Dark Rain
I've often done my input this way for binary files and it always worked perfectly but it creates garbage at the end of the string when using plain text :
    
	std::ifstream inputFile( nomFichier.c_str(), std::ios::in  );
	if ( inputFile.fail())
	{
		//Code that error log obj someday

		return "";
	}

	inputFile.seekg( 0, std::ios::end );
	long fileSize = inputFile.tellg();
	inputFile.seekg( 0, std::ios::beg );

	char *pBuffer = new char[fileSize];
	inputFile.read( pBuffer, fileSize );
	inputFile.close();

	std::string Content = pBuffer;
	delete[] pBuffer;

	return Content;
  
I thought it might be that somehow there was no null character to terminate the string but that's not it either. -Puzzled [edited by - Dark Rain on February 21, 2003 1:05:00 AM]
Sneftel
Sneftel
You were right in the first place. Keep in mind that files are not null-terminated. You need to allocate a buffer of the filesize + 1, and manually terminate the buffer.


But... but that''s what HITLER would say!!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.