Hello, I just started learning c++ and I'm dabbling with file input/output.

But now I have encountered an issue with this code:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
	ifstream inFile("values.txt", ios_base::binary);

	string Value1;
	int Value2;
	int Value3;
	int Value4;

	if(inFile)
	{

		inFile.read((char*)&Value1, sizeof(string));
		inFile.read((char*)&Value2, sizeof(int));
		inFile.read((char*)&Value3, sizeof(int));
		inFile.read((char*)&Value4, sizeof(int));

		inFile.close();
	}

	cout << "Value1= " << Value1 << endl;
	cout << "Value2= " << Value2 << endl;
	cout << "Value3= " << Value3 << endl;
	cout << "Value4= " << Value4 << endl << endl;
	cout << "Loading sucessfull!" << endl;


	system("PAUSE");
	return 0

Everything works until the "return 0", then I get this error:
"Unhandled except at 0x103ead4a (msvcp100d.dll): Access violation reading location 0x003046b4"

I tried looking around on google, but I couldn't find the reason why I get this error or how to fix it.

Any help would be appreciated.

Recommended Answers

All 3 Replies

This line is the problem (line 18):

inFile.read((char*)&Value1, sizeof(string));

You have to understand that string is not like primitive types (like int or float or char), it is a class. This means that an object of class string is not as "simple" as a primitive type. So, simply casting it to a char pointer and writing to it is not going to work. Internally, the string class probably stores pointer to a chain of characters, and when you do this cast and write operation, you are overwriting that internal pointer with some values from the file (you are not filling in the characters). This makes the pointer invalid, and when it is time to delete the string object, the code calls delete on an invalid pointer and you get a crash (access violation).

There are special functions to handle input/output with strings, read() should not be used (unless you are writing a predetermined number of characters to a char array, not a string object). Assuming that the string is contained in your file as a null-terminated string, then this function would be appropriate:

getline(inFile, Value1, '\0'); //this will fill Value1 with characters from inFile until a null-character is found.

If the end of the string is marked by another character besides the null-character (perhaps a newline) than that is the character you put as the third parameter to getline().

I don't think this is correct, I've never seen anyone try to do this, either:

inFile.read((char*)&Value1, sizeof(string));

It just looks like a really bad idea, with the string not being an array of characters and all, what with the functions and what-not in the class.

This line is the problem (line 18):

inFile.read((char*)&Value1, sizeof(string));

You have to understand that string is not like primitive types (like int or float or char), it is a class. This means that an object of class string is not as "simple" as a primitive type. So, simply casting it to a char pointer and writing to it is not going to work. Internally, the string class probably stores pointer to a chain of characters, and when you do this cast and write operation, you are overwriting that internal pointer with some values from the file (you are not filling in the characters). This makes the pointer invalid, and when it is time to delete the string object, the code calls delete on an invalid pointer and you get a crash (access violation).

There are special functions to handle input/output with strings, read() should not be used (unless you are writing a predetermined number of characters to a char array, not a string object). Assuming that the string is contained in your file as a null-terminated string, then this function would be appropriate:

getline(inFile, Value1, '\0'); //this will fill Value1 with characters from inFile until a null-character is found.

If the end of the string is marked by another character besides the null-character (perhaps a newline) than that is the character you put as the third parameter to getline().

Thank you for telling how to solve it and explain it.

It all works now, thanks :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.