I have an object with several text strings as members. I want to write this object to the file all at once, instead of writing each string to file. How can I do that?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can override
operator>>andoperator<<to read/write to stream.Example
Entry structwith some values:Implementation:
Then you open filestream, and for each object you call:
Or output:
Or if you want to use stream
readandwritethen you just replace relevant code inoperators implementation.When the variables are private inside your struct/class then you need to declare
operators as friend methods.You implement any format/separators that you like. When your string include spaces use getline() that takes a string and stream instead of
>>becauseoperator>>uses spaces as delimiters by default. Depends on your separators.