c++读写txt与dat文件
1、创建dat/txt文件(若dat文件不存在时)并向其中写入数据#include <string>#include <iostream>#include <fstream>using namespace std;int main(){ofstream outfile("E:\\myfile.dat&qu
·
1、创建dat/txt文件(若dat文件不存在时)并向其中写入数据
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream outfile("E:\\myfile.dat", ofstream::app);
//ofstream outfile("E:\\myfile.txt", ofstream::app);
string temp = "O \nM \nG \n!"; //写入内容
if(outfile.is_open())
{
outfile<<temp<<"\n";
outfile.close();
}
else
{
cout<<"can not open the file \n"<<endl;
return -1;
}
return 0;
}
2、读取dat/txt文件中的数据
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream infile("E:\\myfile.dat");
//ifstream infile("E:\\myfile.txt");
string temp;
if (!infile.is_open())
{
cout<<"can not open the file \n"<<endl;
return -1;
}
while(getline(infile,temp))
{
cout<<temp<<"\n";
}
infile.close();
return 0;
}
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐



所有评论(0)