A Program to compare two contents of files

void main()
{
char paragraph1[1000];
char paragraph2[1000];
ifstream infile1;
infile1.open("text1.txt");
infile1.getline(paragraph1,1000);

ifstream infile2;
infile2.open("text2.txt");
infile2.getline(paragraph2,1000);
bool compatible=true;
for(int i=0;i<1000;i++)
{
if (paragraph1[i]=!toupper(paragraph2[i]))
compatible=false;
cout<<"The two files are not compatible"<<endl;
break;
}
if(compatible==true)
cout<<"The two files are compatible"<<endl;

}
I am not getting a compiler error. However, when i make the 2 files compatible, I never get the sentence " The 2 files are compatible" printed. I can't figure out if I did a logical mistake or not.
try changing if (paragraph1[i]=!toupper(paragraph2[i])) to if (paragraph1[i]!=toupper(paragraph2[i]))
I tried. It instead printed 2 sentences " The two files are not compatible
and the "2 files are compatible"
replace
if (paragraph1[i]=!toupper(paragraph2[i]))
compatible=false;
cout<<"The two files are not compatible"<<endl;
with
if (paragraph1[i]=!toupper(paragraph2[i]))
{
compatible=false;
cout<<"The two files are not compatible"<<endl;
}
i am sorry, it is to be replaced with
if (paragraph1[i]!=toupper(paragraph2[i]))
{
compatible=false;
cout<<"The two files are not compatible"<<endl;
}
Oh yes, because the if statement will only execute the first statement if no { was put. I have another question. If i want c++ to open the file found on the desktop directory. What should I tell it. And, why can't I save a notepad file in the drive C?
I also want to ask, if the two files were compatible meaning the charecters were the same, however one file is smaller than the other. For example if file1 has this 123abs32hajs and file 2 has 123abs.. The compiler will see the two files as compatible which is not the case since file2 is shorter. What do you propose to solve the problem?
Last edited on
For opening desktop files, you have to give its exact path(eg. in case of Windows 7 the path may be C:/user/..../Desktop).
As for the notepad saving prob, u should be able to save it if u are using ur home pc. If not then the folder may be write protected.
As for strings of different sizes, u can check the length of strings using strlen to check it.
Topic archived. No new replies allowed.