我正在使用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;
}发布于 2011-06-12 05:59:27
我猜你是在Windows上,正遭受着Windows文本模式的危害。将"b"添加到您传递给fopen的标志,即
pSrcFile = fopen ( "test.txt" , "rb" );
pDstFile = fopen ( "result1.txt", "a+b");发布于 2011-06-12 15:15:56
您似乎是在从Dest文件中读取
bytesRead = fread (Dstc , 1, len, pSrcFile);和写到源代码
bytesWritten = fwrite( Src , 1 , len , pDstFile );可能,您必须将Dest更改为Src。
https://stackoverflow.com/questions/6318732
复制相似问题