gpt4 book ai didi

c - fseek() 导致数据重叠

转载 作者:行者123 更新时间:2023-11-30 14:28:18 25 4
gpt4 key购买 nike

我使用 fseek 和 fread 函数读取文件的指定 block ,然后将其写入另一个文件。由于某种原因,在目标文件中,我在其中写入的每个 block 之间有大约 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;
}

最佳答案

我猜您使用的是 Windows,并且正在遭受 Windows 文本模式的危害。将 "b" 添加到传递给 fopen 的标志中,即

pSrcFile = fopen ( "test.txt" , "rb" );
pDstFile = fopen ( "result1.txt", "a+b");

关于c - fseek() 导致数据重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6318732/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com