/*输出流:将内容输出到txt文档中*/
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string file_01 = R"(C:\Users\千木_Muqrel\Desktop\啊对对对.txt)";//创建txt文档
ofstream fout(file_01);//创建输出流
fout << "你是对的" << endl;//输出内容(输出到txt文档中)
if (fout.is_open() != true)//is_open()函数
{
cout << "无法打开文件" << endl;
}//判断是否可以打开文件
fout.close();//关闭输出流
system("pause");
return 0;
}
输出流
/*输入流:将txt文档中的内容输入到程序里*/
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string file_01 = R"(C:\Users\千木_Muqrel\Desktop\啊对对对.txt)";//创建txt文档
ifstream fin(file_01);//创建输入流
if (fin.is_open() != true)
{
cout << "无法打开文件" << endl;
}//判断文件可以不可以打开
string buffer;//定义“缓冲区”存放内容
while (getline(fin, buffer))//读到末尾时getline()返回空,循环结束
{
cout << buffer << endl;//输出存放内容
}
fin.close();//关闭输入流
system("pause");
return 0;
}
输入流