【发布时间】:2014-09-28 12:34:11
【问题描述】:
我在将文本附加到文件时遇到问题。我以附加模式打开一个ofstream,但它仍然不是三行,而是只包含最后一行:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ofstream file("sample.txt");
file << "Hello, world!" << endl;
file.close();
file.open("sample.txt", ios_base::ate);
file << "Again hello, world!" << endl;
file.close();
file.open("sample.txt", ios_base::ate);
file << "And once again - hello, world!" << endl;
file.close();
string str;
ifstream ifile("sample.txt");
while (getline(ifile, str))
cout << str;
}
// output: And once again - hello, world!
那么,附加到文件的正确ofstream 构造函数是什么?
【问题讨论】:
-
在std::ofstream()构造函数文档中有很好的描述。
标签: c++