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

Recommended Answers

All 4 Replies

have you tried it?
If it doesn't work, what does happen?

My guess is you may be trying to read and write to a text file when you are handling binary data.

Firstly, a struct is almost the same as class. The only difference is that evrything is public by default. For a class the default is private

Secondly what you are trying do do with the cast is wrong. The compiler will let you cast anything to anything. You cannot cast your struct to a char*.

Theres a few ways to do what you want. I will illustrate a couple of simple solutions with the write part of your example.

1) Write each member of the struct individually using the << in the filestream, so

cin>>Acc.balance;
    cin>>Acc.Owner;
    File.open("Account.txt",ios::out|ios::trunc);
  
//write the balance and add space
    File<<Acc.balance<<" ";
//write the owner, this wil cal strings << operator
   File<<Acc.Owner;
    File.close();

2) Create << operator in the struct, so your struct will be

struct Account{
  float balance;
  string Owner;
  friend fstream& operator<<(fstream& f, Account& acc)
  {
    f<<acc.balance<<" "<<acc.Owner;
    return f;
  }

};

then in the write bit

cin>>Acc.balance;
    cin>>Acc.Owner;
    File.open("Account.txt",ios::out|ios::trunc);
  
//write the struct contents
    File<<Acc;

    File.close();

You can do something similar for the read.

have you tried it?
If it doesn't work, what does happen?

My guess is you may be trying to read and write to a text file when you are handling binary data.

Yes, I have tried it. It won't read in the string from the structure. I get no runtime errors other than that. I'm able to write it to the file and everything, but not read it in. It only reads in the float, however, If I exchange the string with a character array, it works just fine.

Firstly, a struct is almost the same as class. The only difference is that evrything is public by default. For a class the default is private

I knew that one, but that's not imporatant. :mrgreen:

Secondly what you are trying do do with the cast is wrong. The compiler will let you cast anything to anything. You cannot cast your struct to a char*.

This I don't know. I'm not too familiar with type casting yet; the cast I did was because of what I read in the fstream tutorial on this site. It did seem a bit awkward to me, but it worked so I didn't question it too much, and was just waiting to learn more on it when I finish this file stuff.

Thanks for the examples. I'll have to learn to overload operators, but that's fine. I appreciate the help.

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.