-
compairing files
I need to know how i would go about compairing 2 files. I have 2 files and i need to know how to check if the CONTENTS in them are equil. I have tryed putting it in a string the compairing the 2 strings but that dosnt seem to work any one know how i oculd go about this? any help is much apreciated.
-
Something like this -
Code:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream file1("firstfile",ios::binary);
ifstream file2("secondfile",ios::binary);
char buff1[255]={0};
char buff2[255]={0};
bool theSame = 0;
while(!file1.eof() && !file2.eof() && theSame==0)
{
file1.read(buff1,sizeof(buff1));
file2.read(buff2,sizeof(buff2));
if (memcmp(buff1,buff2,sizeof(buff1)))
theSame=1;
}
if(theSame==0)
cout << "Files are the same\n";
else
cout << "Files are different\n";
return 0;
}
You could optimise it, check the file size before the loop to prevent having to check the whole file and put in some error checking.
-
If I remember correctly memcmp will just compare size of memory used but not contents of that memory. Obviously if the size is not the same then the contents can't be the same either, but the size could be the same and the contents different. I think comparison using strcmp() if you use c_style string or the equal operator if you use STL style string or char by char comparison is the way to go if memcmp returns 0. If it isn't working posting your code would be the next step in requesting help.
-
hmmm i guess i should have been more spicific, i cant post code because of a security issue, its a fairly big project im working on. Any way, i will try to post code that is altered but still asks the same question. What i am trying to do is write 10 random numbers to two diffrent files. These files are then to be read, and compared. My main problem now is that when i make the 2 copys one file always comes out slightly diffrent than the other, they are in diffrent directorys but the end of one it will have a musical note site, or a # sign and then a letter, (the signs and letters seem to be random) has any one had this problem or know whats going on? i'll post some code to tell you what im doing.
srand(time(NULL));
char filename[16];
int i;
int b=0;
int x;
bool v=0;
bool y=0;
char info[50050];
cout<<"filename>";
cin>>filename;
ofstream file1("a:\\filename.txt" ios::trunc);
ofstream file2(filename, ios::trunc); //
file2<<filename<<endl;
file1<<filename<<endl;
for(i=0; i<10; i++)
{
x=0;
x=rand()%10;
cout<<".";
file1<<x;
file2<<x;
}
cout<<"finished";
file1.close();
file2.close();
return 0;
}
i have tryed making one file and copying it but for some odd reason file2 always seems to add stuff to the end.
-
> My main problem now is that when i make the 2 copys one file always comes out slightly diffrent than the other,
Probably because you're using eof() to test for the end of file, and not checking for the EOF result from the function which actually reads the file. As a result, you end up doing one more write (of garbage data) than you ought to do (the read fails with EOF, but you do a write anyway).
This C code copies files.
Code:
void copy_file ( char *from, char *to ) {
FILE *ifp = fopen( from, "rb" );
FILE *ofp = fopen( to, "wb" );
char buff[BUFSIZ];
int n;
while ( (n=fread(buff,1,BUFSIZ,ifp)) > 0 ) {
fwrite( buff, 1, n, ofp );
}
fclose( ifp );
fclose( ofp );
}