在對我的另一個答案的評論中,你說過這個任務是你經常會做的事,你也表達了對自動化的期望。這是一個小型的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