Is reading and writing strings inside of structures possible, or must you use character arrays? Here's what I've tried; no compiler errors, but it doesn't display the text:
struct Account{
float balance;
string Owner;
};
int main(int argc, char *argv[])
{
fstream File;
Account Acc;
int row;
cout<<"Open or New? ";
cin>>row;
if(row==1){ //READ
File.open("Account.txt",ios::in);
if(!File){
cout<<"error opening file"<<endl;
return -1;
}
while(!File.eof()){
File.read((char*)&Acc,sizeof(Acc));
}
cout<<Acc.balance<<endl;
cout<<Acc.Owner<<endl;
File.close();
}
else{ //WRITE
cout<<"Input balance then owner:";
cin>>Acc.balance;
cin>>Acc.Owner;
File.open("Account.txt",ios::out|ios::trunc);
File.write((char*)&Acc,sizeof(Acc));
File.close();
}
system("PAUSE");
return 0;
}
If you must use character arrays, then I would appreciate a short explanation of why.
Thanks in advance