| | |
Read and writing strings from structures
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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:
If you must use character arrays, then I would appreciate a short explanation of why.
Thanks in advance
C++ Syntax (Toggle Plain 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
Squirrel Prodigy
•
•
Join Date: Dec 2004
Posts: 13
Reputation:
Solved Threads: 1
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
2) Create << operator in the struct, so your struct will be
then in the write bit
You can do something similar for the read.
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)
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
C++ Syntax (Toggle Plain Text)
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
C++ Syntax (Toggle Plain Text)
cin>>Acc.balance; cin>>Acc.Owner; File.open("Account.txt",ios::out|ios::trunc); //write the struct contents File<<Acc; File.close();
Last edited by alc6379; Dec 7th, 2004 at 6:27 pm. Reason: disabled smilies
•
•
•
•
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.
Squirrel Prodigy
•
•
•
•
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
•
•
•
•
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*.
Thanks for the examples. I'll have to learn to overload operators, but that's fine. I appreciate the help.
Squirrel Prodigy
![]() |
Similar Threads
- Tutorial: Understanding ASP classes (ASP)
- Read strings into array (C)
- strings, arrays, length program (C++)
- Please help me compare strings (Java)
Other Threads in the C++ Forum
- Previous Thread: iostream in C?
- Next Thread: Help me understand Pointers
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






