Hi,

Im pretty new to c++ programming so plz dont be offended if i ask "stupid" questions.

Im want to open a file named "Alexander".
The name of the file is stored in an Ansisting.
So i want to add an ".txt" extension to open the file.
Then i want to read a line form the file and store it in an string named StringFromFile.

The only problem was that i get an Acces violation. So i searched for some treads on this site and found the following tread:
http://www.daniweb.com/forums/newthread.php?do=newthread&f=8

After reading it i figured out that this char array may be stored in read-only memory. So my question is: is the acces violation generated because om trying to add ".txt" to char* temp?

I already have a solution by using char temp[] but i would still like to understand why the acces violation was genereted!

AnsiString filename = "Alexander";
	std:string StringFromFile;
	char* temp = filename.c_str();

	strcat(temp, ".txt");

	ifstream myfile3;
	myfile3.open (temp);
	getline(myfile3, StringFromFile);
	myfile3.close();
	delete[] temp;

For strcat to work the destination should be large enough to hold the concatenated output. temp points to a memory location that is big enough to hold the filename and nothing more.

For strcat to work the destination should be large enough to hold the concatenated output. temp points to a memory location that is big enough to hold the filename and nothing more.

oke thanks for ur reply! i think i understand it now. so when i use strcpy to try to add ".txt" to temp im writing to memory that was not reserved for temp? because the program still worked, it only gave me the acces violation when the programm ended an tried to free the memory.

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.