Skip to main content
gamedev.net
🔒 Locked

getline method runtime error

Started by 3dmodelerguy 3 replies 700+ views
Original Post
3dmodelerguy
3dmodelerguy
Here is my code and the runtime error I am getting. I mean the code comes fine and i am not sure why this is not working right, can anyone help me? code

	while ( !inConfigFile.eof() )
	{
!!!!! THE ERROR OCCURS AT THE LINE BELOW THIS STATEMENT !!!!!
		inConfigFile.getline( buffer, MAX_CONFIG_FILE_LINE_SIZE, '/n' ); 
		current_line = buffer;
		config_contents.push_back( current_line );

error: Unhandled exception at 0x0041c1a3 in ConfigFiel.exe: 0xC0000005: Access violation writing location 0x00000000.
Rayno
Rayno
I would debug it, and make sure buffer is pointing to something valid.

Post where it declare/initialize buffer and I could probably find your problem.
3dmodelerguy
3dmodelerguy
void CConfigFile::ReadFromFile()
{
ifstream inConfigFile( "config.ini", ios::in );

if ( !inConfigFile )
{
cout << "Unable to locate file" << endl;
exit( 1) ;
}

string current_line;
char *buffer = NULL;

while ( !inConfigFile.eof() )
{
inConfigFile.getline( buffer, MAX_CONFIG_FILE_LINE_SIZE, '/n' );
current_line = buffer;
config_contents.push_back( current_line );
}
}

and the
list config_contents;

is declare in the private part of the class.
Rayno
Rayno
You have to initialize buffer. In other words, getline doesn't create a string and return a pointer to it, it just tries to write to the pointer you give it. And the pointer you gave it is null.

Here's how it should look:
string current_line; char *buffer = new char[MAX_CONFIG_FILE_LINE_SIZE];while ( !inConfigFile.eof() ){    inConfigFile.getline( buffer, MAX_CONFIG_FILE_LINE_SIZE, '/n' );    current_line = buffer;    config_contents.push_back( current_line );}delete [] buffer;
kman12
kman12
Also, shouldn't that /n be a \n?

kman

Topic Locked

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

Sign in to reply to this topic.