0

我使用 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;
}
4

2 回答 2

2

我猜你是在 Windows 上并且遭受 Windows 文本模式的弊端。添加"b"到您传递给的标志fopen,即

pSrcFile = fopen ( "test.txt" , "rb" );
pDstFile = fopen ( "result1.txt", "a+b");
于 2011-06-11T21:59:27.157 回答
0

看来您正在从Dest文件中读取

bytesRead = fread (Dstc , 1, len, pSrcFile);

并写入源代码

bytesWritten = fwrite( Src , 1 , len , pDstFile );

可能,您必须更改DestSrc.

于 2011-06-12T07:15:56.050 回答