kainhart
asked on
Old Question Revisited, (comparing file data)
I asked a question about how to compare to files if they have exacly the same content. For example in windows you copy a file and change it's name so it's basically the same file with a different name. The answer I got was to make a the function listed below. I used this function for a while until I got a nice hex editor and I found out that even if the files were somewhat different, that the function was still saying that they were duplicates. Here is a listing of my function with a small driver.
#include <fstream.h>
#include <string.h>
bool CompareFileData(const char *Fil1, const char *Fil2)
// Returns TRUE if the files have the same content
// Returns FALSE otherwise
{
fstream inDataFile1(Fil1,ios::in || ios::binary);
fstream inDataFile2(Fil2,ios::in || ios::binary);
const int BufferSize = 1024;
char Buffer1[BufferSize];
char Buffer2[BufferSize];
int RedLen1; // what are these are for?
int RedLen2;
while (true)
{
inDataFile1.read(Buffer1,B ufferSize) ;
RedLen1 = inDataFile1.gcount(); // what does this function do?
inDataFile2.read(Buffer2,B ufferSize) ;
RedLen2 = inDataFile2.gcount();
if (RedLen1 != RedLen2)
return false;
if (RedLen1 == 0)
return true;
if (memcmp(Buffer1,Buffer2,Re dLen1))
return false;
}
}
void main()
{
char fileName1[120] = {"G:\\download\\testfolder \\copy1.jp g\0"};
char fileName2[120] = {"G:\\download\\testfolder \\copy2.jp g\0"};
if(CompareFileData(&fileNa me1[0], &fileName2[0]))
cout << "\n\nThese files are the same.\n";
else
cout << "\n\nThese files are not the same.\n";
}
I'm really frustrated with all the different available functions for reading file buffers and such, and I don't know why I'm using char arrays when I'm trying to deal with binary data. If somebody can figure out why this function is not working and explain it to me I would greatly appreciate it. Thanks
#include <fstream.h>
#include <string.h>
bool CompareFileData(const char *Fil1, const char *Fil2)
// Returns TRUE if the files have the same content
// Returns FALSE otherwise
{
fstream inDataFile1(Fil1,ios::in || ios::binary);
fstream inDataFile2(Fil2,ios::in || ios::binary);
const int BufferSize = 1024;
char Buffer1[BufferSize];
char Buffer2[BufferSize];
int RedLen1; // what are these are for?
int RedLen2;
while (true)
{
inDataFile1.read(Buffer1,B
RedLen1 = inDataFile1.gcount(); // what does this function do?
inDataFile2.read(Buffer2,B
RedLen2 = inDataFile2.gcount();
if (RedLen1 != RedLen2)
return false;
if (RedLen1 == 0)
return true;
if (memcmp(Buffer1,Buffer2,Re
return false;
}
}
void main()
{
char fileName1[120] = {"G:\\download\\testfolder
char fileName2[120] = {"G:\\download\\testfolder
if(CompareFileData(&fileNa
cout << "\n\nThese files are the same.\n";
else
cout << "\n\nThese files are not the same.\n";
}
I'm really frustrated with all the different available functions for reading file buffers and such, and I don't know why I'm using char arrays when I'm trying to deal with binary data. If somebody can figure out why this function is not working and explain it to me I would greatly appreciate it. Thanks
first version :
#include <fstream.h>
#include <string.h>
bool CompareFileData(const char *Fil1, const char *Fil2)
// Returns TRUE if the files have the same content
// Returns FALSE otherwise
{
fstream inDataFile1(Fil1,ios::in || ios::binary);
fstream inDataFile2(Fil2,ios::in || ios::binary);
char *Buffer1;
char *Buffer2;
int RedLen1;
int RedLen2;
inDataFile1.seekg(2);
RedLen1=inDataFile1.tellg ();
inDataFile1.seekg (0);
inDataFile2.seekg(2);
RedLen2=inDataFile2.tellg ();
inDataFile2.seekg (0);
if (RedLen1 != RedLen2)
return false;
if (RedLen1 == 0)
return true;
Buffer1=new char[RedLen1+1];
inDataFile1.get(Buffer1,Re dLen1);
Buffer2=new char[RedLen1+1];
inDataFile2.get(Buffer2,Re dLen2);
if (strcmp(Buffer1,Buffer2))
return false;
return true;
}
void main()
{
char fileName1[120] = {"G:\\download\\testfolder \\copy1.jp g\0"};
char fileName2[120] = {"G:\\download\\testfolder \\copy2.jp g\0"};
if(CompareFileData(fileNam e1, fileName2))
cout << "\n\nThese files are the same.\n";
else
cout << "\n\nThese files are not the same.\n";
}
//end
second version:
#include <fstream.h>
#include <string.h>
bool CompareFileData(const char *Fil1, const char *Fil2)
// Returns TRUE if the files have the same content
// Returns FALSE otherwise
{
fstream inDataFile1(Fil1,ios::in || ios::binary);
fstream inDataFile2(Fil2,ios::in || ios::binary);
char Buffer1;
char Buffer2;
int RedLen1;
int RedLen2;
inDataFile1.seekg(2);
RedLen1=inDataFile1.tellg ();
inDataFile1.seekg (0);
inDataFile2.seekg(2);
RedLen2=inDataFile2.tellg ();
inDataFile2.seekg (0);
if (RedLen1 != RedLen2)
return false;
if (RedLen1 == 0)
return true;
while (!inDataFile1.eof()){
inDataFile1>>Buffer1;
inDataFile2>>Buffer2;
if (Buffer1!=Buffer2)
return false;
}
return true;
}
void main()
{
char fileName1[120] = {"G:\\download\\testfolder \\copy1.jp g\0"};
char fileName2[120] = {"G:\\download\\testfolder \\copy2.jp g\0"};
if(CompareFileData(fileNam e1, fileName2))
cout << "\n\nThese files are the same.\n";
else
cout << "\n\nThese files are not the same.\n";
}
//end
check them there maybe bugs about the length of the file but i think it should work
#include <fstream.h>
#include <string.h>
bool CompareFileData(const char *Fil1, const char *Fil2)
// Returns TRUE if the files have the same content
// Returns FALSE otherwise
{
fstream inDataFile1(Fil1,ios::in || ios::binary);
fstream inDataFile2(Fil2,ios::in || ios::binary);
char *Buffer1;
char *Buffer2;
int RedLen1;
int RedLen2;
inDataFile1.seekg(2);
RedLen1=inDataFile1.tellg ();
inDataFile1.seekg (0);
inDataFile2.seekg(2);
RedLen2=inDataFile2.tellg ();
inDataFile2.seekg (0);
if (RedLen1 != RedLen2)
return false;
if (RedLen1 == 0)
return true;
Buffer1=new char[RedLen1+1];
inDataFile1.get(Buffer1,Re
Buffer2=new char[RedLen1+1];
inDataFile2.get(Buffer2,Re
if (strcmp(Buffer1,Buffer2))
return false;
return true;
}
void main()
{
char fileName1[120] = {"G:\\download\\testfolder
char fileName2[120] = {"G:\\download\\testfolder
if(CompareFileData(fileNam
cout << "\n\nThese files are the same.\n";
else
cout << "\n\nThese files are not the same.\n";
}
//end
second version:
#include <fstream.h>
#include <string.h>
bool CompareFileData(const char *Fil1, const char *Fil2)
// Returns TRUE if the files have the same content
// Returns FALSE otherwise
{
fstream inDataFile1(Fil1,ios::in || ios::binary);
fstream inDataFile2(Fil2,ios::in || ios::binary);
char Buffer1;
char Buffer2;
int RedLen1;
int RedLen2;
inDataFile1.seekg(2);
RedLen1=inDataFile1.tellg ();
inDataFile1.seekg (0);
inDataFile2.seekg(2);
RedLen2=inDataFile2.tellg ();
inDataFile2.seekg (0);
if (RedLen1 != RedLen2)
return false;
if (RedLen1 == 0)
return true;
while (!inDataFile1.eof()){
inDataFile1>>Buffer1;
inDataFile2>>Buffer2;
if (Buffer1!=Buffer2)
return false;
}
return true;
}
void main()
{
char fileName1[120] = {"G:\\download\\testfolder
char fileName2[120] = {"G:\\download\\testfolder
if(CompareFileData(fileNam
cout << "\n\nThese files are the same.\n";
else
cout << "\n\nThese files are not the same.\n";
}
//end
check them there maybe bugs about the length of the file but i think it should work
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
to ntdragon: you can't use
strcmp for test binary data(becouse
must test 0x00 and after this too.
see, that original text(of nietod?) use
memcmp.
strcmp for test binary data(becouse
must test 0x00 and after this too.
see, that original text(of nietod?) use
memcmp.
ASKER
Thanks a lot I made this change and it made me function work like a charm.
i mean you can read the two files to two
char* and then compaire them with strcmp
i'm not sure about your code i"ll go to my second comp and rewrite the func for you for now as i understand
the:
int RedLen1; // what are these are for?
int RedLen2;
//will keep the length of files
RedLen1 = inDataFile1.gcouent(); // what does this function do?
//gets the length of the file