944,028 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4577
  • C++ RSS
Dec 6th, 2004
0

Read and writing strings from structures

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  1. struct Account{
  2. float balance;
  3. string Owner;
  4. };
  5.  
  6. int main(int argc, char *argv[])
  7. {
  8. fstream File;
  9. Account Acc;
  10. int row;
  11. cout<<"Open or New? ";
  12. cin>>row;
  13. if(row==1){ //READ
  14. File.open("Account.txt",ios::in);
  15. if(!File){
  16. cout<<"error opening file"<<endl;
  17. return -1;
  18. }
  19. while(!File.eof()){
  20. File.read((char*)&Acc,sizeof(Acc));
  21. }
  22. cout<<Acc.balance<<endl;
  23. cout<<Acc.Owner<<endl;
  24. File.close();
  25. }
  26. else{ //WRITE
  27. cout<<"Input balance then owner:";
  28. cin>>Acc.balance;
  29. cin>>Acc.Owner;
  30. File.open("Account.txt",ios::out|ios::trunc);
  31. File.write((char*)&Acc,sizeof(Acc));
  32. File.close();
  33. }
  34. system("PAUSE");
  35. return 0;
  36. }

If you must use character arrays, then I would appreciate a short explanation of why.

Thanks in advance
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
SquirrelProdigy is offline Offline
24 posts
since Nov 2004
Dec 6th, 2004
0

Re: Read and writing strings from structures

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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Dec 6th, 2004
0

Re: Read and writing strings from structures

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

C++ Syntax (Toggle Plain Text)
  1. cin>>Acc.balance;
  2. cin>>Acc.Owner;
  3. File.open("Account.txt",ios::out|ios::trunc);
  4.  
  5. //write the balance and add space
  6. File<<Acc.balance<<" ";
  7. //write the owner, this wil cal strings << operator
  8. File<<Acc.Owner;
  9. File.close();

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

C++ Syntax (Toggle Plain Text)
  1. struct Account{
  2. float balance;
  3. string Owner;
  4. friend fstream& operator<<(fstream& f, Account& acc)
  5. {
  6. f<<acc.balance<<" "<<acc.Owner;
  7. return f;
  8. }
  9.  
  10. };

then in the write bit

C++ Syntax (Toggle Plain Text)
  1. cin>>Acc.balance;
  2. cin>>Acc.Owner;
  3. File.open("Account.txt",ios::out|ios::trunc);
  4.  
  5. //write the struct contents
  6. File<<Acc;
  7.  
  8. File.close();
You can do something similar for the read.
Last edited by alc6379; Dec 7th, 2004 at 6:27 pm. Reason: disabled smilies
Reputation Points: 10
Solved Threads: 1
Newbie Poster
britt_boy is offline Offline
13 posts
since Dec 2004
Dec 7th, 2004
0

Re: Read and writing strings from structures

Quote originally posted by jwenting ...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
SquirrelProdigy is offline Offline
24 posts
since Nov 2004
Dec 7th, 2004
0

Re: Read and writing strings from structures

Quote originally posted by britt_boy ...
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:

Quote originally posted by britt_boy ...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
SquirrelProdigy is offline Offline
24 posts
since Nov 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: iostream in C?
Next Thread in C++ Forum Timeline: Help me understand Pointers





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC