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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
#include <iostream>
#include <fstream>
#include <string.h>
#include <ostream>
#include <stdio.h>
#include <string>
using namespace std;
int main()
{
//Open the two files//
string file1, file2;
ifstream Mary1, Mary2;
file1="Mary1.txt" ;
file2="Mary2.txt" ;
Mary1.open( file1.c_str(), ios::binary ); //c_str() returns C-style string pointer
Mary2.open( file2.c_str(), ios::binary );
// Check for failures //
if (Mary1.fail())
{cout << "Couldn't open the file " << file1 <<endl;
return 0;
}
if (Mary2.fail())
{cout << "Couldn't open the file " << file2 << endl;
return 0;
}
// Read lines and increment line number counter //
char string1[256], string2[400];
int j = 0, error_count =0;
while(!Mary1.eof())
{
Mary1.getline(string1,256);
Mary2.getline(string2,256);
j++;
if(strcmp(string1,string2) != 0)
{
cout << j << "- the strings are not equal " << endl;
cout << " file1: " << string1 << endl;
cout << " file2: " << string2 << endl;
error_count++;
}
}
if (error_count > 0) {
cout << "Files are different"<< endl;}
else {cout << "Files are the same"<< endl;}
//---------- compare number of lines in both files ------------------//
int c1,c2;
c1 = 0;
c2 = 0;
string str;
while(!Mary1.eof())
{
getline(Mary1,str);
c1++;
}
Mary1.clear(); //add
Mary1.seekg(0,ios::beg); //add
// Mary1.clear(); // set new value for error control state //
// Mary2.seekg(0,ios::beg);
while(!Mary2.eof())
{
getline(Mary2,str);
c2++;
}
Mary2.clear();
Mary2.seekg(0,ios::beg);
if(c1 != c2)
{
cout << "Different number of lines in files!" << "\n";
cout << file1 << " has " << c1 << " lines and "<< file2 <<" has " << c2 << " lines" << "\n";
return 0;}
}
|