compare two file txt

Hi everybody
My homework is write the program to compare two files .txt and figure out they are different or they are same. If they are different, the program have to point out the line, which are different. I complete the first part, but I have no idea how to do the second part. I haven't been taught getline yet, so I cannot use
so far i have
#include<iostream>
#include<fstream>
#include<cctype>
#include<string>

using namespace std;

int main()
{
// Declare the variables

char song, song1, a, b;

int count(0), k(0);

// create the file object

ifstream bd, bdf;

// open the files

bd.open("C:\\ver_b.txt");

bdf.open("C:\\ver_a.txt");

// check the files

if(bd.fail() && bdf.fail())
{
cout<<"Error opening file"<<endl;

system("pause");

return 0;
}
bd.get(song);
bdf.get(song1);
while(!bd.eof()&&!bdf.eof())
{
if (song!=song1)
k = k + 1;

bd.get(song);
bdf.get(song1);
}

if (k!=0)
{
cout<<" they are the different "<<endl;

}
else
cout<<" They are same"<<endl;


bd.close();

bdf.close();

system("pause");

return 0;
}

This code can point out the two files are different or same.

Any idea for part 2. I think about two loop, loop one is for each character in one sentence and loop two is for each sentence until end of file. Please help me.
closed account (28poGNh0)
Can you give us what's in the text files
I think the txt file is doesn't matter because you can make up the files

For example

Good Morning everyone Good Morning everyone Good Morning everyone Good Morning everyone Good Morning everyone Good Morning everyone

and

Good Morning everyone Good Morning everyone Good Morning everyone Good Morning everyone Good Morning everyone Good Mornint everyone

they are different. how to point out the line that different ? That is my job.
You need only to poin out line?
1
2
3
4
5
6
7
8
9
10
11
12
//ifstream file1, file2
std::string string1, string2;
int i = 1;
while(file1.good() && file2.good()) {
    std::getline(file1, string1);
    std::getline(file2, string2);
    if(string1 != string2) {
        std::cout << "first difference encountered on " << i << " line";
        break;
    }
    ++i;
}
closed account (28poGNh0)
Dont use system("anything");read thread abouve
return 0 means the success of the program

I maid three text file
ver_a.txt contains

Good Morning everyone
Good Morning everyone
Good Morning everyone
Good afternoon everyone
Good Morning everyone
Good Morning everyone

ver_b.txt contains

Good Morning everyone
Good Morning everyone
Good Morning everyone
Good Morning everyone
Good Morning everyone
Good Morning everyone

ver_c.txt countains

Good Morning everyone
Good Morning everyone
Good Morning everyone
Good Morning everyone
Good Morning everyone
Good Morning everyone

This is the program I seggest

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# include <string>
# include <fstream>
# include <iostream>
# include <limits.h>
# include <stdlib.h>
using namespace std;

int main()
{
    string str,str2;

    ifstream bd, bdf;

    bd.open("C:\\ver_a.txt");
    bdf.open("C:\\ver_b.txt");


    if(bd.fail() && bdf.fail())
    {
        cout << "Error opening file" << endl;

        cin.ignore(INT_MAX);

        exit(EXIT_FAILURE);
    }

    while(bd&&bdf)
    {
        if(bd&&bdf)
        {
            str += (char)bd.get();
            str2 += (char)bdf.get();
        }
    }

    if (str.compare(str2)==0)
        cout << "They are same" << endl;
    else
        cout << "They are different " << endl;

    bd.close();

    bdf.close();

    cin.ignore(INT_MAX);

    return 0;
}


output : They are different

if you compare between ver_b.txt and ver_c.txt they will be same

also
Dont use system("anything");read thread abouve
and return 0;means the success of the program not its failure
Thank you very much for all reply

I already have this result. How can I output the line that different ? I cannot use getline to solve the problem since the professor hasn't taught yet.
I mean the number of line
closed account (28poGNh0)
How about now

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# include <string>
# include <fstream>
# include <iostream>
# include <limits.h>
# include <stdlib.h>
using namespace std;

int main()
{
    string str,str2;

    ifstream bd, bdf;

    bd.open("C:\\ver_a.txt");
    bdf.open("C:\\ver_b.txt");


    if(bd.fail() && bdf.fail())
    {
        cout << "Error opening file" << endl;

        cin.ignore(INT_MAX);

        exit(EXIT_FAILURE);
    }

    while(bd&&bdf)
    {
        if(bd&&bdf)
        {
            str += (char)bd.get();
            str2 += (char)bdf.get();
        }
    }

    if (str.compare(str2)==0)
        cout << "They are same" << endl;
    else
    {
        cout << "They are different " << endl;

        int i=0,j=1;
        string str3;

        while(str[i]&&str2[i])
        {
            if(str[i]==str2[i])
                str3 += str[i];
            else
            {
                i = 0;
                while(str3[i])
                    if(str3[i++]=='\n')
                        j++;
                cout << "The number of the line that contain the deference is : " << j << endl;
                exit(EXIT_SUCCESS);
            }i++;
        }
    }

    bd.close();

    bdf.close();

    cin.ignore(INT_MAX);

    return 0;
}
have you tested it yet ? I cannot because the black box just pop up and then disappear, I don't see anything. If it works, I will try to understand your code and then revise it. Thank you very much.
closed account (28poGNh0)
can you replace cin.ignore(INT_MAX);with getch();

also put # include <conio.h> on top

I am waiting your answer
It doesn't fix the code. Get the same result like the previous one.
closed account (28poGNh0)
what about now

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# include <string>
# include <fstream>
# include <iostream>
# include <limits.h>
# include <stdlib.h>
# include <conio.h>
using namespace std;

int main()
{
    string str,str2;

    ifstream bd, bdf;

    bd.open("C:\\ver_a.txt");
    bdf.open("C:\\ver_b.txt");


    if(bd.fail() && bdf.fail())
    {
        cout << "Error opening file" << endl;

        cin.ignore(INT_MAX);

        getch();
        exit(EXIT_FAILURE);
    }

    while(bd&&bdf)
    {
        if(bd&&bdf)
        {
            str += (char)bd.get();
            str2 += (char)bdf.get();
        }
    }

    if (str.compare(str2)==0)
        cout << "They are same" << endl;
    else
    {
        cout << "They are different " << endl;

        int i=0,j=1;
        string str3;

        while(str[i]&&str2[i])
        {
            if(str[i]==str2[i])
                str3 += str[i];
            else
            {
                i = 0;
                while(str3[i])
                    if(str3[i++]=='\n')
                        j++;
                cout << "The number of the line that contain the deference is : " << j << endl;
                getch();
                exit(EXIT_SUCCESS);
            }i++;
        }
    }

    bd.close();

    bdf.close();

    getch();

    return 0;
}
can you explain for me a little bit. There are some line I don't understand

while(bd&&bdf) \\ What is it mean ? Why don't you use eof ?

if(bd&&bdf) \\ I don't understand this one either, the condition in parenthesis.

if (str.compare(str2)==0) \\ Can I use if(str == str2) to replace for this one?

while(str[i]&&str2[i]) \\ I don't get this one also.

thank you very much
The newest code run good but it miss 1 different line.
while(str[i]&&str2[i]) // how can you assign a int number for a string
#include<iostream>
#include<fstream>
#include<cctype>
#include<string>

using namespace std;

int main()
{
// Declare the variables

char song, song1, a,b;

int count(1), k(0);

// create the file object

ifstream bd, bdf;

// open the files

bd.open("C:\\ver_b.txt");

bdf.open("C:\\ver_c.txt");

// check the files

if(bd.fail() && bdf.fail())
{
cout<<"Error opening file"<<endl;

system("pause");

return 0;
}
while(!bd.eof()&&!bdf.eof())
{
while(!bd.eof()&&!bdf.eof())
{
bd.get(song);
a = song;
bdf.get(song1);
b = song1;
if(a == b && a == '\n' && b == '\n')
{
count = count +1;
}
if(/*a != '\n' && b != '\n' && */a!=b)
{
k = k + 1;
break;
}
}

//cout<<k<<endl;
//cout<<count<<endl;

if (k == 0)
cout<<"they are same"<<endl;
if (k != 0)
{
cout<<"They are different. "<<"Line "<<count<<" is different"<<endl;
}

}
bd.close();

bdf.close();

system("pause");

return 0;
}

That is so far I have. This code can print out the different lines. But there are two different character on the same line, that's why I get this result

They are different. Line 25 is different
They are different. Line 25 is different
They are different. Line 33 is different
They are different. Line 33 is different
They are different. Line 41 is different
Press any key to continue . . .

In addition, I have no idea why the program inform that line 41 is different. indeed, it isn't. I guess that's loop fault but I don't know how to fix it. I want to eliminate the duplicate and the last line. Any idea to do that ? Thanks


Topic archived. No new replies allowed.