在输入文件中找到所有相同的连续行组。在输出文件中放置每个这样的组的信息,包括重复行的文本,重复的数量和该组开始的行号。 自己设计输出文件的格式。
#include<fstream>
#include<iostream>
#include<cstring>
#include<iomanip>
using namespace std;
char data[1000], temp[1000];
int main () {
ifstream infile;
infile.open("test.in");
ofstream outfile;
outfile.open("test.out");
infile.getline(data, 1000);
int start = 1, end = 1, line = 1;
strcpy(temp, data);
while(infile) {
infile.getline(data, 1000);
line++;
if(strcmp(temp, data) == 0) {
end++;
} else {
if(end > start) {
outfile << left << setw(5) << start << setw(5) << end << setw(5) << end - start + 1 << setw(30) << temp << endl;
}
start = line;
end = line;
memset(temp, 0, sizeof(temp));
strcpy(temp, data);
}
}
infile.close();
outfile.close();
return 0;
}