I want to perform I/O operation in c++. I want to 开发者_StackOverflow中文版store a pointer to fstream object and using that same fstream I want to read and write to that file. Is it possible without using two different objects i.e ifstream for reading and ofstream for writing.
Yes, an fstream
is specifically intended to support both reading and writing (it derives from both ifstream
and ofstream
).
Yes, fstream
can be used for reading and writing. Is this what you want to accomplish?
// Your fstream object
std::fstream a("coco.txt");
// Buffer
char foo[100];
// Write
a<<"Hello"<<endl;
// Rewind
a.seekg(0,ios::beg);
// Read
a>>foo;
// Display
std::cout<<foo;
// Clean up
a.close();
精彩评论