Krešimir Čulina
Krešimir Čulina

Reputation: 93

Finding different strings in two files - C++

I'm trying to export all lines from second file that aren't in the first one. The order of the lines doesn't matters, I just want to find ones that aren't in the first file already and save them to difference.txt.

Example:

firstfile.txt

This is first line
This is second line
This is third line

secondfile.txt

This is first line
This is some line
This is third line

Now compare them...

difference.txt

This is some line


This is what I came up so far. I know I need to loop through all lines in the second file and compare each of that line with each line of the first file. It's not making any sense to me why it isn't working

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

Any help would be appreciated. Thanks for reading this block of text.

Upvotes: 2

Views: 1861

Answers (3)

kendotwill
kendotwill

Reputation: 2020

If you want to do it manually then it sounds like you don't need a c++ program but you can do this from the command line using grep.

grep -vxFf firstfile.txt secondfile.txt > difference.txt

Upvotes: 0

sasha.sochka
sasha.sochka

Reputation: 14715

Here is a simple but much more effective and idiomatic solution using std::set:

std::ifstream file1("firstfile.txt");
std::set<std::string> str_in_file1;
std::string s;
while (std::getline(file1, s)) { 
    str_in_file1.insert(s);
}
file1.close();
std::ifstream file2("secondfile.txt");
std::ofstream file_diff("diff.txt");
while (std::getline(file2, s)) { 
    if (str_in_file1.find(s) == str_in_file1.end()) {
        file_diff << s << std::endl;
    }
}
file2.close();
file_diff.close();

Also, you might want to use a tool called diff. It does exactly what you are trying to do.

Upvotes: 3

Jim Lewis
Jim Lewis

Reputation: 45035

This code doesn't do what you think it does:

if (strcmp(stringf.c_str(), search.c_str()))
{
    matches += 1;
}

strcmp() returns 0 when the strings are equal, but your code will not increment matches in that case.

Upvotes: 3

Related Questions