2

最初のファイルにない 2 番目のファイルからすべての行をエクスポートしようとしています。行の順序は重要ではありません。最初のファイルにまだ含まれていない行を見つけて、difference.txt に保存したいだけです。

例:

firstfile.txt

これが 1 行
目です これが 2 行目
です これが 3 行目です

セカンドファイル.txt

これは 1 行目
です これはいくつかの行
です これは 3 行目です

今それらを比較してください...

違い.txt

これはいくつかの行です


これが私がこれまでに思いついたものです。2 番目のファイルのすべての行をループし、その各行を最初のファイルの各行と比較する必要があることはわかっています。なぜそれが機能しないのか、私には意味がありません

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;
    }
}

どんな助けでも大歓迎です。このテキスト ブロックをお読みいただきありがとうございます。

4

3 に答える 3