代码:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream readFile;
ofstream writeFile;
char ch;
readFile.open("a.txt", ios::in);
writeFile.open("b.txt", ios::out);
while (readFile.get(ch))
writeFile.put(ch);
readFile.close();
writeFile.close();
cout << "Finish!" << endl;
return 0;
}
运行结果:
代码:
#include <iostream>
#include <fstream>
#include<cstring>
using namespace std;
int main()
{
ifstream readFile;
ofstream writeFile;
char ch[100];
readFile.open("a.txt", ios::in);
writeFile.open("b.txt", ios::out);
while (!readFile.eof())
{
readFile.getline(ch,100,'\n');
writeFile.write(ch,strlen(ch));
writeFile.write("\n",1);
}
readFile.close();
writeFile.close();
cout << "Finish!" << endl;
return 0;
}
运行结果:
学习心得:
用成员函数readFile,writeFile读写二进制文件。while (!readFile.eof())表示readFile.eof()为假表示未遇到文件结束符。