hello, im newbie in c++ and i have some newbie questions.
erm here's the problem ive been thinking :
#include <iostream>
#include <fstream>
using namespace std;
struct Info
{
string name;
int age;
};
int main()
{
Info person1, person2;
fstream writefile("output.txt", ios::out | ios::binary);
person1.name = "sendy";
person1.age = 18;
writefile.write(reinterpret_cast<char*>(&person1), sizeof(person));
writefile.close();
fstream readfile("outputt.txt", ios::in | ios::binary);
readfile.read(reinterpret_cast<char*>(&person2), sizeof(person2));
cout<<"name = "<<person2.name<<endl;
cout<<"age = "<<person2.age<<endl;
readfile.close();
return 0;
}
erm i already know that the problem is with the "person1.name" and "person2.name" because its a string class object, means its a pointer inside a structure which ive been trying to convert into a char*, some people said it would work if i used char arrays for person1.name and person2.name,
then i try to read more from my beginner c++ e-book , also search some keywords in google but i still havent got it how to convert pointer inside a structure into a char pointer..
how to fix this problem?
note : sorry if i ask alot, im just curios :D