我使用 fseek 和 fread 函数读取文件的指定块,然后将其写入另一个文件。由于某种原因,在目标文件中,我在其中写入的每个块之间都有大约 20 个字节的重叠。
谁能帮我确定这些垃圾的来源?它肯定是由 fseek 函数引起的,但我不知道为什么。
FILE *pSrcFile;
FILE *pDstFile;
int main()
{
int buff[512], i;
long bytesRead;
pSrcFile = fopen ( "test.txt" , "r" );
pDstFile = fopen ( "result1.txt", "a+");
for(i = 0; i < 5; i++)
{
bytesRead = _readFile ( &i, buff, 512);
_writeFile( &i, buff, bytesRead);
}
fclose (pSrcFile);
fclose (pDstFile);
}
int _readFile (void* chunkNumber, void* Dstc, long len)
{
int bytesRead;
long offset = (512) * (*(int*)chunkNumber);
fseek( pSrcFile, offset, SEEK_SET);
bytesRead = fread (Dstc , 1, len, pSrcFile);
return bytesRead;
}
int _writeFile (void* chunkNumber, void const * Src, long len)
{
int bytesWritten;
long offset = (512) * (*(int*)chunkNumber);
bytesWritten = fwrite( Src , 1 , len , pDstFile );
return bytesWritten;
}