1 #include <iostream>
2 #include <fstream>
3 #include <string>
4 using namespace std;
5 //文本文件 写文件
6 void write_()
7 {
8 //以下一一种建立并写入TXT文件的方式
9 ofstream file;
10 string filepath, filename, s;
11 filepath = ("e:\\"); //文件位置
12 filename = "a.txt"; //文件名
13 filename = filepath + filename; //文件路径
14 file.open(filename, 0x02);//建立文件
15 file << "姓名:张秋山" << endl;
16 file << "性别:男" << endl;
17 file << "年龄:21" << endl;
18 file.close(); //关闭文件
19
20
21 //1.包含头文件 fstream
22 //#include <fstream>
23 //2.创建流对象
24 ofstream ofs;
25 //3.指定打开文件
26 ofs.open("e:\\test.txt", 0x02);//ios::out
27 //4.写内容
28 ofs << "姓名:张秋山" << endl;
29 ofs << "性别:男" << endl;
30 ofs << "年龄:21" << endl;
31 //5.关闭文件
32 ofs.close();
33 }
34 int main()
35 {
36 cout << "写文件:" << endl << endl;
37 write_();
38 system("pause");
39 return 0;
40 }
原文链接: https://www.cnblogs.com/RedWetPlace/p/16261235.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/189102
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!