编辑代码

/*本文档记录 C++文本文件 的 写操作*/
/*文件分两种:
1.文本文件:ASCII码写的,软件打开,可懂
2.二进制文件:01写的,打开也看不懂
*/
/*  文本文件的操作:
1.头文件 fstream
2.ofstream写文件类;ifstream读文件类;fstream读写文件类
3.open("文件路径",mode)
mode是打开方式:写文件:ios::out;读文件:ios::in;
io 输入输出 s是stream的意思
*/
#include <iostream>
#include <fstream>
using namespace std;

void test(){
    ofstream ofs;//创建写文件流对象
    ofs.open("test.txt",ios::out);
    //打开和.cpp文件一个目录下的test.txt文件,不存在的话,创建它,写入文件
    ofs<<"这是一个测试文件"<<endl;//利用输出文件流 写内容到文件中
    ofs<<"第二行"<<endl;
    ofs.close();
}
int main(){
    test();
}