【发布时间】:2015-03-04 22:45:50
【问题描述】:
使用<fstream> 库打开流并将流添加到现有文件test.rtf 时,我使用以下行:
char data[100];
// open a file in write mode.
ofstream outfile;
outfile.open("test.rtf");
if (outfile.is_open()) { cout << "file is open" << endl; }
cout << "Writing to the file" << endl;
cout << "Enter your name: ";
cin.getline(data, 100);
// write inputted data into the file.
outfile << data << endl;
使用 ifstream 读取时,输入的行显示正确。问题是输出文件没有修改,我添加的行没有保存。这个问题可能听起来很愚蠢,但这是我无法解决的问题。
【问题讨论】:
-
您确定要写入的文件与您之后查看的文件相同吗?你说如果通过 ifstream 读取内容就在那里——你通过什么其他方法独立检查文件内容?
-
基本上,我使用 ifstream 类和 file_name >> 数据来读取内容