fseek() 导致数据重叠

发布于 2024-11-15 00:58:56 字数 922 浏览 3 评论 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;
}

Im reading a specified chunk of a file with fseek and fread functions and then writing it to another file. For some reason in the destination file I get about 20 bytes overlap between every chunk written in it.

Could anyone, please, help me identifying the source of this garbage? It is definitely caused by the fseek function, but I cant figure out why.

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

国产ˉ祖宗 2024-11-22 00:58:56

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

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

I'm guessing you're on Windows and suffering from the evils of Windows' text mode. Add "b" to the flags you pass to fopen, i.e.

pSrcFile = fopen ( "test.txt" , "rb" );
pDstFile = fopen ( "result1.txt", "a+b");
揽清风入怀 2024-11-22 00:58:56

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

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

并写入源文件

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

可能,您必须将 Dest 更改为 Src

It seems you are reading from Dest file

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

and writing to source

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

Probably, you have to change Dest to Src.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文