c++文件读写,追加,覆盖,fstream的使用
c++primer(5版)第八章IO库
#include <iostream>
#include <string>
#include<fstream>
using namespace std;
int main()
{
//注意“/”
string fin="D:/cpp/tempvscode/in.txt";
string fout="D:/cpp/tempvscode/out.txt";
//两种写入方式
//方式1:追加
// ofstream out1(fout,ofstream::app);//追加末尾
//方式2:覆盖
//写入流
ofstream out1(fout);//覆盖
//读入流
ifstream in1(fin);
//读入一行存储到s2中
string s2;
while (getline(in1,s2))
{ //输出到文件out.txt中
out1<<s2<<endl;
}
return 0;
}