向文件追加数据
Coddy C++ 文件处理 课程的第 13 课,共 23 课。
.put(char) 方法会向文件写入单个字符,适合添加换行符或格式符号等单个字符。
另一方面,.write(string, size n) 会将字符串中的 n 个字符写入文件,适合处理较大的数据块。
ofstream output;
output.open("example.txt");
output.put('H');
output.put('I');
output.put('!');
output.put('\n');
char* message = "Hello!";
output.write(message, strlen(message));挑战
简单编写一个程序,打开名为 output.txt 的文件,从标准输入读取 3 个字符,然后将这些字符追加到一个字符串中,每个字符之间用空白分隔
*不要更改默认代码*
亲手试试
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
// 在这里输入你的代码
// 不要修改下面的代码!
ifstream file("output.txt");
string line;
while(!file.eof()){
getline(file, line);
cout << line;
}
file.close();
}C++ 文件处理中的所有课
自己练习: C++ 在线编译器