C ++如何检查2个相似文件中不存在的单词
C++ How to Check what words aren't in 2 similar files
我试图找到一种方法来检查两个不同的文件,并从第二个文件中获取所有不在第一个文件中的行..但恰恰相反。
我尝试了解决这个问题的可能性,但没有...
这是代码:
int main(int argc, char *argv[])
{
setlocale(LC_ALL, "");
char username[UNLEN+1];
DWORD username_len = UNLEN+1;
GetUserName(username, &username_len);
stringstream buffer;
buffer << "C:\Users\" << username << "\Desktop\";
stringstream buffer2;
buffer2 << "C:\Users\" << username << "\Desktop\Legit.txt";
stringstream buffer3;
buffer3 << "C:\Users\" << username << "\Desktop\Unlegit.txt";
stringstream buffer4;
buffer4 << "C:\Users\" << username << "\Desktop\result.txt";
string results = buffer4.str();
int offset;
int num;
num = 1;
string search;
string linea;
string legit;
string unlegit;
string line;
cout << "Is the Legit.txt file at '" << buffer.str() << "'? [Y/N]: ";
cin >> legit;
if (legit == "Y" || legit == "y"){
}else if(legit == "N" || legit == "n"){
return 0;
}else{
cout << "\n.";
return 0;
}
string legitfile = buffer2.str();
cout << "\nIs the Unlegit.txt file at '" << buffer.str() << "'? [Y/N]: ";
cin >> unlegit;
if (unlegit == "Y" || unlegit == "y"){
}else if(unlegit == "N" || unlegit == "n"){
return 0;
}else{
cout << "\n";
return 0;
}
string unlegitfile = buffer3.str();
ifstream file(legitfile.c_str());
if(file.is_open()){
while(getline(file, line)){
ifstream MyFile(unlegitfile.c_str());
if(MyFile.is_open()){
while(!MyFile.eof()){
getline(MyFile,linea);
if((offset = linea.find(line, 0)) != string::npos) {
cout << "\n[" << num << "]" << " Word Found: " << line << "\n";
num++;
fstream result(results.c_str());
result << line << "\n";
result.close();
}
}
MyFile.close();
}
}
file.close();
return 0;
}else{
cout << "\nThe file '" << legitfile << "' does not exist.";
cout << "\nThe file '" << unlegitfile << "' does not exist.";
}
}
正如我所说,这段代码检查哪些单词在两个(第一个和第二个)文件中是相等的,一旦找到,就将它们写入第三个文件,有一种方法可以做相反的事情(检查两个文件和得到不等于的话)?非常感谢!
我是论坛和C++的新手,如果我有任何错误,请原谅。 (也很抱歉我的英语不好)。
linux 上有一个名为 diff 的程序,它可以完成您希望在 C++ 中完成的工作。
它是用C写的,所以你可以复制它的源代码=P
for (;; cmp->file[0].buffered = cmp->file[1].buffered = 0)
{
/* Read a buffer's worth from both files. */
for (f = 0; f < 2; f++)
if (0 <= cmp->file[f].desc)
file_block_read (&cmp->file[f],
buffer_size - cmp->file[f].buffered);
/* If the buffers differ, the files differ. */
if (cmp->file[0].buffered != cmp->file[1].buffered
|| memcmp (cmp->file[0].buffer,
cmp->file[1].buffer,
cmp->file[0].buffered))
{
changes = 1;
break;
}
/* If we reach end of file, the files are the same. */
if (cmp->file[0].buffered != buffer_size)
{
changes = 0;
break;
}
}
取自 ftp://mirrors.kernel.org/gnu/diffutils/diffutils-3.0.tar.gz > src/analyze.c
这类问题的经典解决方案是使用哈希 table 集合来表示第一个文件中的所有单词。然后在迭代第二个文件中的项目时,查阅由第一个文件构造的集合。在 C++ 中,std::unordered_set
会很好。
#include <unordered_set>
using namespace std;
unordered_set<string> firstFileSet;
unordered_set<string> missingFromSecondFileSet;
string line;
while(!firstfile.eof())
{
getline(firstfile,line);
firstFileSet.insert(line);
}
然后对于第二个文件中的每个单词,使用第二个集合来跟踪缺少的单词。
while(!secondfile.eof())
{
getline(secondfile,line);
if (firstFileSet.find(line) != firstFileSet.end())
{
missingFromSecondFileSet.insert(line);
}
else
{
firstFileSet.erase(line);
}
}
以上运行后,firstFileSet
包含第一个文件中第二个文件中不存在的所有行。 missingFromSecondFileSet
包含第二个文件中不在第一个文件中的所有行:
for (auto &s : firstFileSet)
{
cout << s << " was in the first file, but not the second" << endl;
}
for (auto &s : missingFromSecondFileSet)
{
cout << s << " was in the second file, but not the first" << endl;
}
我试图找到一种方法来检查两个不同的文件,并从第二个文件中获取所有不在第一个文件中的行..但恰恰相反。
我尝试了解决这个问题的可能性,但没有...
这是代码:
int main(int argc, char *argv[])
{
setlocale(LC_ALL, "");
char username[UNLEN+1];
DWORD username_len = UNLEN+1;
GetUserName(username, &username_len);
stringstream buffer;
buffer << "C:\Users\" << username << "\Desktop\";
stringstream buffer2;
buffer2 << "C:\Users\" << username << "\Desktop\Legit.txt";
stringstream buffer3;
buffer3 << "C:\Users\" << username << "\Desktop\Unlegit.txt";
stringstream buffer4;
buffer4 << "C:\Users\" << username << "\Desktop\result.txt";
string results = buffer4.str();
int offset;
int num;
num = 1;
string search;
string linea;
string legit;
string unlegit;
string line;
cout << "Is the Legit.txt file at '" << buffer.str() << "'? [Y/N]: ";
cin >> legit;
if (legit == "Y" || legit == "y"){
}else if(legit == "N" || legit == "n"){
return 0;
}else{
cout << "\n.";
return 0;
}
string legitfile = buffer2.str();
cout << "\nIs the Unlegit.txt file at '" << buffer.str() << "'? [Y/N]: ";
cin >> unlegit;
if (unlegit == "Y" || unlegit == "y"){
}else if(unlegit == "N" || unlegit == "n"){
return 0;
}else{
cout << "\n";
return 0;
}
string unlegitfile = buffer3.str();
ifstream file(legitfile.c_str());
if(file.is_open()){
while(getline(file, line)){
ifstream MyFile(unlegitfile.c_str());
if(MyFile.is_open()){
while(!MyFile.eof()){
getline(MyFile,linea);
if((offset = linea.find(line, 0)) != string::npos) {
cout << "\n[" << num << "]" << " Word Found: " << line << "\n";
num++;
fstream result(results.c_str());
result << line << "\n";
result.close();
}
}
MyFile.close();
}
}
file.close();
return 0;
}else{
cout << "\nThe file '" << legitfile << "' does not exist.";
cout << "\nThe file '" << unlegitfile << "' does not exist.";
}
}
正如我所说,这段代码检查哪些单词在两个(第一个和第二个)文件中是相等的,一旦找到,就将它们写入第三个文件,有一种方法可以做相反的事情(检查两个文件和得到不等于的话)?非常感谢!
我是论坛和C++的新手,如果我有任何错误,请原谅。 (也很抱歉我的英语不好)。
linux 上有一个名为 diff 的程序,它可以完成您希望在 C++ 中完成的工作。
它是用C写的,所以你可以复制它的源代码=P
for (;; cmp->file[0].buffered = cmp->file[1].buffered = 0)
{
/* Read a buffer's worth from both files. */
for (f = 0; f < 2; f++)
if (0 <= cmp->file[f].desc)
file_block_read (&cmp->file[f],
buffer_size - cmp->file[f].buffered);
/* If the buffers differ, the files differ. */
if (cmp->file[0].buffered != cmp->file[1].buffered
|| memcmp (cmp->file[0].buffer,
cmp->file[1].buffer,
cmp->file[0].buffered))
{
changes = 1;
break;
}
/* If we reach end of file, the files are the same. */
if (cmp->file[0].buffered != buffer_size)
{
changes = 0;
break;
}
}
取自 ftp://mirrors.kernel.org/gnu/diffutils/diffutils-3.0.tar.gz > src/analyze.c
这类问题的经典解决方案是使用哈希 table 集合来表示第一个文件中的所有单词。然后在迭代第二个文件中的项目时,查阅由第一个文件构造的集合。在 C++ 中,std::unordered_set
会很好。
#include <unordered_set>
using namespace std;
unordered_set<string> firstFileSet;
unordered_set<string> missingFromSecondFileSet;
string line;
while(!firstfile.eof())
{
getline(firstfile,line);
firstFileSet.insert(line);
}
然后对于第二个文件中的每个单词,使用第二个集合来跟踪缺少的单词。
while(!secondfile.eof())
{
getline(secondfile,line);
if (firstFileSet.find(line) != firstFileSet.end())
{
missingFromSecondFileSet.insert(line);
}
else
{
firstFileSet.erase(line);
}
}
以上运行后,firstFileSet
包含第一个文件中第二个文件中不存在的所有行。 missingFromSecondFileSet
包含第二个文件中不在第一个文件中的所有行:
for (auto &s : firstFileSet)
{
cout << s << " was in the first file, but not the second" << endl;
}
for (auto &s : missingFromSecondFileSet)
{
cout << s << " was in the second file, but not the first" << endl;
}