C++输出内容到文件的最基本方法

由于我现在对文件的操作还不多,只是想把输出的内容存到文件备用,也没有特殊要求,所以只要最基本的文件存储操作就可以了,以下是最基本文本文件存储方法:
1、头文件 :#include<fstream>

2、命名空间:using namespace std;

3、创建对象:ofstream outfile;

4、对象并联文件:outfile.open("aaa123.txt"); 文件会保存在项目目录下。加路径outfile.open("e:\\aaa123.txt");

5、输出内容:如:outfile << "输出的内容" << endl;

6、关闭文件:outfile.close();

完整代码:

#include <iostream>
#include<fstream>

using namespace std;

int main() {
	ofstream outfile;
	outfile.open("e:\aaa123.txt");
	outfile << "输出的内容" << endl;
	outfile << "输出" << endl;
	outfile.close();
	return 0;
}

aaa123.txt文件内容:

猜你喜欢

转载自blog.csdn.net/kim5659/article/details/114191288
今日推荐