c++指定路径创建文件

 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 }

 

posted @ 2022-05-12 10:00  雾枫  阅读(3063)  评论(0编辑  收藏  举报