C++,文本文件,写文件操作

代码演示:

#include 
using namespace std;

//1、包含头文件
#include


void test() {
    //2、创建流对象
    ofstream ofs;
    //3、打开文件(默认源文件路径)
    ofs.open("test.txt", ios::out);
    //4、写数据
    ofs << "1、包含头文件" << endl;
    ofs << "2、创建流对象" << endl;
    ofs << "3、打开文件" << endl;
    ofs << "4、写数据" << endl;
    ofs << "5、关闭文件" << endl;
    //5、关闭文件
    ofs.close();
}


int main()
{
    test();
}

运行截图:

C++,文本文件,写文件操作_第1张图片

 

文件截图:

C++,文本文件,写文件操作_第2张图片

 

你可能感兴趣的:(c++,开发语言)