我正在尝试从第二个文件中导出不在第一个文件中的所有行。行的顺序无关紧要,我只想找到那些已经不在第一个文件中的行并将它们保存到differ.txt。
例子:
第一个文件.txt
这是第一行
这是第二行
这是第三行
第二个文件.txt
这是第一行
这是一些行
这是第三行
现在比较一下...
差异.txt
这是一些线
这就是我到目前为止提出的。我知道我需要遍历第二个文件中的所有行并将每一行与第一个文件的每一行进行比较。为什么它不起作用对我来说没有任何意义
void compfiles()
{
std::string diff;
std::cout << "-------- STARTING TO COMPARE FILES --------\n";
ifstream file2;
file2.open("C:\\\\firstfile.txt",ios::binary);
//---------- compare two files line by line ------------------
std::string str;
int j = 0;
while(!file2.eof())
{
getline(file2, str);
if(!CheckWord(str))
{
cout << "appending";
diff.append(str);
diff.append("\n");
}
j++;
}
ofstream myfile;
myfile.open ("C:\\\\difference.txt");
myfile << diff;
myfile.close();
}
bool CheckWord(std::string search)
{
ifstream file;
int matches = 0;
int c = 0;
file.open("C:\\\\secondfile.txt",ios::binary);
std::string stringf;
while(!file.eof())
{
getline(file, stringf);
if(strcmp(stringf.c_str(), search.c_str()))
{
matches += 1;
}
c++;
}
if(matches == 0)
{
return false;
}
else
{
return true;
}
}
任何帮助,将不胜感激。感谢您阅读此文本块。