reading from file problem

Hi,

I have the following code to read two lines of a file and then put the data into variables of the object that called the function.
1
2
3
4
5
6
7
8
9
10
11
void Artikel::readFromFile() 
{
	char * filename = "artikel.txt";
	ifstream istr(filename);
	char temp [80];
	istr.getline(temp, 80);
	cout<<"temp is : " << temp <<endl;
	m_pnaam = temp;
	istr>>m_price;
	istr.close();
}


my cout which helps me debugging this function gave the correct result for temp.
Now when I exit this function and return to main. If I print out m_pnaam there, it prints something really weird. tube-like characters (a lot), smiley face, a square...

Any help would be appreciated.
What is m_pnaam?
I don't see what could it be other than a char*, but please next time note stuff like that.
temp is a local variable, so, when the function ends, it may be overwritten by some other data.
To prevent that you need to create a new char array and copy the data
1
2
m_pnaam = new char[80];
memcpy(m_pnaam, temp, 80);

Hope this helps
Topic archived. No new replies allowed.