在对我的另一个答案的评论中,你说过这个任务是你经常会做的事,你也表达了对自动化的期望。这是一个小型的C++程序,应该可以工作:像prg file1 file2
一样使用它来查找file1中的行,并使用file2中缺少的密钥。要从file1中丢失行,请切换参数的顺序:prg file2 file1
。
#include <iostream>
#include <string>
#include <map>
#include <fstream>
using namespace std;
typedef map< string, string > tMap;
bool readFileIntoMap(string fn, tMap &m)
{
ifstream inFile(fn, std::ios::in);
if(!inFile.good()){
std::cout << "Could not open " << fn << std::endl;
return false;
}
string key, aLine;
string::size_type pos;
while (inFile) {
getline(inFile, aLine) ;
pos = aLine.find(';');
if(pos != string::npos) {
key = aLine.substr(0, pos);
} else {
key = "-1";
}
m[ key ] = aLine; // map key to complete line
} // of while
return true;
}
// check for each key of first file: if the key is present in the
// second file, if not: report the line from the first file
void findMissingKeys(tMap &leftMap, tMap &rightMap)
{
string leftKey;
for(auto &leftElem : leftMap) {
leftKey = leftElem.first;
auto it = rightMap.find(leftKey);
if(it == rightMap.end()) {
// report missing line in second file
cout << leftElem.second << endl;
}
}
}
int main(int argc, char* argv[]) {
if (argc != 3) {
cerr << "Please provide exactly two filenames as argument!" << endl;
cerr << "Program will dump lines with a key present in first and missing in second file." << endl;
return 1;
}
tMap m1, m2;
readFileIntoMap(argv[1], m1);
readFileIntoMap(argv[2], m2);
findMissingKeys(m1,m2);
}
我会尝试此代码现在... 我不明白为什么我的问题被评为负面... – Orion310591
如果有人读这个,我用DevC++(50mb安装)编译代码,我不得不添加“-std = c工具>编译器选项“中的”++ 11“(不含引号)。 – Orion310591
这工作!我向你们致敬!再次感谢我希望我有这个15代表所需,所以我可以upvote您的评论。 我刚刚发现我可以使用Shift +右键点击内部文件夹来获得选项“在这里打开命令窗口”,并给我提示该位置的cmd提示符...再次感谢代码。 – Orion310591