15,440
社区成员
发帖
与我相关
我的任务
分享
#include <fstream>
#include <string>
using namespace std;
int main(){
ifstream infile("in.txt");
ofstream outfile("out.txt", ios::app);
char c;
while(infile.get(c)){
outfile <<c;
}
infile.close();
outfile.close();
return 0;
}
题主用getline()只读取了每一行的数据, 但没读到换行符;
而用get()则读取到了文本中的每一个字符(包括换行符和空格), 所以结果复制的文本格式一致.#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main() {
int i = 0;
string temp;
ifstream infile;
ofstream outfile0;
ofstream outfile1;
infile.open("C:\\Users\\waiting\\Desktop\\test.txt", ios::in);
outfile0.open("C:\\Users\\waiting\\Desktop\\out0.txt", ios::app);
outfile1.open("C:\\Users\\waiting\\Desktop\\out1.txt", ios::app);
while (!infile.eof()) {
getline(infile, temp, '\n');
i++;
if (i%2) outfile1 << temp << endl;
else outfile0 << temp << endl;
}
infile.close();
outfile0.close();
outfile1.close();
return 0;
}