简体   繁体   中英

How to compare two files in C++?

I need to compare two files in C++, and increase the value of a counter for each "equality" found.

My code:

ifstream flux("C:\\Users\\Matthieu\\Desktop\\test.txt",ios::in);
char ligne[100];

ifstream flux2("base_fonctions.txt", ios::in);
char ligne2[100];

while (flux.getline(ligne, 100)) {

    bool found = false;

    while (flux2.getline(ligne2, 100)) {

        if (ligne == ligne2) {

            found = true;
            count = count + 0.5;

        }
    }
    if (found==false) {

        count = count;
    }
}

return count;

I would like to compare line 1 of the test.txt file with all the lines of the base_functions.txt file, line 2 of the test file with all the lines of the second file, etc.
Currently, my function constantly returns zero when there are indeed equalities between the two files?

Your comparation does not work because it is not comparing the lines, it is comparing the array pointers and they are never going to be equal in this scenario. Your count should not be 0, but I suppose it is an integer and it is adding 0 to it.

To compare two lines you can use strcmp from <cstring> .

Also, this attribution it not necessary as it does nothing to count.

if (found==false) {

    count = count;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM