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>
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.
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.
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.
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