#include
#include
#include
#include
#include
#ifndef MAX_PATH
#define MAX_PATH 260
#endif
int CompareFile(const char* fname1, const char* fname2)
{
int result = 0;
const int fcount = 2;
const char* fname[fcount] = { fname1, fname2 };
FILE* file[fcount] = { 0 };
long size[fcount] = { 0 };
for(int i = 0; i < fcount; ++i)
{
try
{
file[i] = fopen(fname[i], "rb");
}
catch(...) { }
if(file[i] == NULL)
{
result = 1;
printf("can not open file: %s\n", fname[i]);
break;
}
else
{
fseek(file[i], 0, SEEK_END);
size[i] = ftell(file[i]);
fseek(file[i], 0, SEEK_SET);
}
}
if(size[0] != size[1])
result = 1;
const long buffer_size = 1024;
unsigned char buffer[fcount][buffer_size] = { 0 };
for(long l = 0; l < size[0] && result == 0; l += buffer_size)
{
long rcount = 0;
for(int i = 0; i < fcount; ++i)
{
rcount = size[0] - l;
rcount = (rcount >= buffer_size) ? buffer_size : rcount;
if(rcount != fread(buffer[i], 1, rcount, file[i]))
{
printf("read file error: %s\n", fname[i]);
result = 1;
break;
}
}
if(result == 0)
{
if(0 != memcmp(buffer[0], buffer[1], rcount))
{
result = 1;
}
}
}
for(int i = 0; i < fcount; ++i)
{
if(file[i])
fclose(file[i]);
}
return result;
}
void main()
{
const int count = 2;
const char inFmtStr[count][MAX_PATH] = { "first file: ", "second file: " };
char fname[count][MAX_PATH] = { 0 };
for(int i = 0; i < count; ++i)
{
printf(inFmtStr[i]);
scanf("%s", fname[i]);
}
int result = CompareFile(fname[0], fname[1]);
printf("result: %d\n", result);
_getch();
}
int a,b,t;
if (a=b) t=0;
else t=1;
printf("%d,t);