C++读、写、文件操作!

#include <iostream>
#include <fstream>

using namespace std;
int main()
{
ifstream in;
in.open("file1.txt"); //打开一个文件
char c;
if(!in) //提示
{
cerr<<"open failure!\n"<<endl;
return 0;
}
while(in>>c) //从文件最开始读。把读到的字符放到in
{
cout<<c; //读一个显示一个。
}
cout<<endl; //结束
in.close(); //关闭文件
return 0;
}
从文件里写入
#include <iostream>
#include <fstream>

using namespace std;
int main()
{
ofstream out; //
out.open("file1.txt"); //打开一个文件
if(!out) //提示
{
cerr<<"open failure!\n"<<endl;
return 0;
}
for(int i=0;i<10;i++)
{
out<<i;
}
cout<<endl; //结束
out.close(); //关闭文件
return 0;
}



原文地址:https://www.cnblogs.com/qingjoin/p/2279558.html