开发者

io operation using same stream object

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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜