workingmailing@163.com wrote:
Does Windows have any API for two files compare?
No, because there’s no obvious “one way” for such an API to work. Do
you just return pass/fail? Do you return the location of the first
mismatch? Do you return all mismatches?
Fortunately, it’s trivial to write your own.
I rethink it:
Readfile read some data(partial of the file) from U disk, and Writefile to a file located in PC’s SATA HDD/SSD drive.
Or the other direction, read some data from PC’s sata HDD/SSD drive, and write to u disk.
In this circumstance, I can only check data integrity after total file copy is finished.
Then you can compare the file in udisk, and file in sata drive.
Otherwise, you read partial of the file into a buffer(DDR memory), for data integrity, you need another “correct” data for compare with the data in the readed buffer, but there no correct data here.
My original design is:
the readed buffer compare with a “correct data”, if it is not same, then I send a special USB packet, for analyzer to trig.
But it seems, this can not implemented, cause no “correct data” exist.
I cannot believe how complicated you are making this. You are thinking
about this problem WAY too hard.
Please take a step back, and ask yourself “What is the problem I am
trying to solve here?” If you are trying to copy a file and verify that
the file was copied correctly, then you just answered the question on
how to do the copy.
// Copy
CreateFile( hSource, GENERIC_READ, … );
CreateFile( hDest, GENERIC_WRITE, … );
std::vector buffer1(BUFFERSIZE);
while( forever )
{
DWORD dwActual;
ReadFile( hSource, &buffer1[0], buffer1.size(), &dwActual,
nullptr );
if( !dwActualBytes )
break;
WriteFile( hDest, &buffer1[0], dwActual, &dwActual, nulltpr );
}
CloseHandle( hDest );
// Compare
SetFilePointer( hSource, 0, nullptr, FILE_BEGIN );
CreateFile( hDest, GENERIC_READ, … );
std::vector buffer2(BUFFERSIZE);
int Position = 0;
while( forever )
{
DWORD dwActual1, dwActual2;
ReadFile( hSource, &buffer1[0], buffer1.size(), &dwActual1,
nullptr );
ReadFile( hDest, &buffer2[0], buffer2.size(), &dwActual2, nullptr );
if( dwActual1 != dwActual2 )
{
printf( “One file ended early, read %d and %d at %d\n”,
dwActual1, dwActual2, Position );
break;
}
if( memcmp( &buffer1[0], &buffer2[0], dwActual1 ) != 0 )
{
printf( “Files differ in block at %d\n”, Position );
break;
}
Position += dwactual1;
}
CloseHandle( hSource );
CloseHandle( hDest );
If you actually want to do your comparing while you are doing the copy,
you can also do that, by opening the destination file as
GENERIC_READ|GENERIC_WRITE and using SetFilePointer, but I think that’s
a stupid option, and it will reduce your performance significantly.
int Position = 0;
while( forever )
{
DWORD dwActual1, dwActual2;
ReadFile( hSource, &buffer1[0], buffer1.size(), &dwActual1,
nullptr );
WriteFile( hDest, &buffer1[0], &dwActual1, &dwActual2, nullptr );
SetFilePointer( hDest, Position, nullptr, FILE_BEGIN );
ReadFile( hDest, &buffer2[0], buffer2.size(), &dwActual2, nullptr );
if( dwActual1 != dwActual2 )
{
printf( “One file ended early, read %d and %d at %d\n”,
dwActual1, dwActual2, Position );
break;
}
if( memcmp( &buffer1[0], &buffer2[0], dwActual1 ) != 0 )
{
printf( “Files differ in block at %d\n”, Position );
break;
}
Position += dwactual1;
}
See? It’s just not that hard. You simply have to think about your
problem in words, and turn the words into code.
> My question about createfile, readfile, writefile to implement the file copy is:
> does the 2nd read and 2nd write is after the first read/write,
> for example, first read/write from file offset 0 - 64K
> and does the second read/write is from file offset 64K to 128K?
> Because I can’t file the offset parameter in readfile and writefile API.
The file system stores a current offset with your file handle. Every
time you read or write, it advances that offset. You can use
SetFilePointer to get or change that offset
–
Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.