C++读写文本文件
#include "stdafx.h"
#include <iostream>
using namespace std;
#include <fstream>
//读写文本文件
int _tmain(int argc, _TCHAR* argv[])
{
fstream ifs("src.txt", ios::in);
if (!ifs)
cout << "open error" << endl;
fstream ofs("dest.txt", ios::out | ios::trunc);
if (!ofs)
cout << "open error" << endl;
//char data;
//while (ifs.get(data), !ifs.eof()) //读文件的方法
//{
// //cout << data << endl;
// ofs << data ; //写入文件
//}
char buf[1024];
while (ifs.getline(buf, 1024, '\n'), !ifs.eof())
{
ofs << buf << endl;
}
ifs.close();
ofs.close();
return 0;
}