fwrite()错过了写一个字节 [英] fwrite() misses writing a byte

查看:169
本文介绍了fwrite()错过了写一个字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,它将大量字节写入

二进制文件,这只是一些编组数据。


int len = Data.size (); // arrary size

for(int i = 0; i< len; ++ i)

fwrite(& Data [i],1,1,f );


现在运行了很长时间并且推动了数百万字节,一旦错过了写fData的最后一个字节,它就是
。然后进一步推动

字节再次正确。由于我没有使用

fwrite的返回值,我认为有一些错误可能是由于缓冲区

溢出在流中。这很难重现并发生在

Solaris上。

任何帮助都将不胜感激。

I have an application which writes huge number of bytes into the
binary files which is just some marshalled data.

int len = Data.size(); //arrary size
for (int i = 0; i < len; ++i)
fwrite(&Data[i], 1, 1, f);

now after running this for long time and pushing millions of bytes, It
once misses writing the last byte of fData. Then the further push of
bytes is again correct. As i am not using the return value for the
fwrite I think there is some error which could be due to buffer
overrun in the stream. This is hard to reproduce and happening on the
Solaris.
Any help will be appreciated.

推荐答案

he*********@gmail.com 写道:
he*********@gmail.com wrote:

我有一个应用程序,它将大量字节写入

二进制文件,这只是一些编组数据。


int len = Data.size(); // arrary size
I have an application which writes huge number of bytes into the
binary files which is just some marshalled data.

int len = Data.size(); //arrary size



您好像使用C ++。如果是这样,那么< news:comp.lang.c ++可能是你问题的更好的地方。

You seem to using C++. If so then <news:comp.lang.c++might be a better
venue for your question.


for(int i = 0; i< len; ++ i)

fwrite(& Data [i],1,1,f);
for (int i = 0; i < len; ++i)
fwrite(&Data[i], 1, 1, f);



你不能一次写多个字节吗?这可能是写入大量字节的低效方法。另外考虑使用

C ++ fstream对象,因为你还是在使用C ++。

Can''t you write more than one byte at a time? This is probably an
inefficient method for writing large number of bytes. Also consider using
C++ fstream object for this, since you''re using C++ anyway.


现在运行了很长时间后推送数百万字节,一旦错过了写fData的最后一个字节,它就是
。然后进一步推动

字节再次正确。由于我没有使用

fwrite的返回值,我认为有一些错误可能是由于缓冲区

溢出在流中。这很难重现并发生在

Solaris上。

任何帮助都将不胜感激。
now after running this for long time and pushing millions of bytes, It
once misses writing the last byte of fData. Then the further push of
bytes is again correct. As i am not using the return value for the
fwrite I think there is some error which could be due to buffer
overrun in the stream. This is hard to reproduce and happening on the
Solaris.
Any help will be appreciated.



没有理由可以忽略这些I / O函数的状态值,除非是一次性程序。 fwrite返回成功写入的

数据项的数量,因此计算的数量比预期的短,意味着错误发生了b / b
。最可能的原因是磁盘配额超过了



无论如何你应该将它重新发布到comp.lang.c ++,因为你正在使用C ++。

There is no excuse to ignore the status values that these I/O functions
report except perhaps in throwaway programs. fwrite returns the number of
data items successfully written, so a shorter count than expected means
that there was an error. The most likely cause is that the disk quota was
exceeded.

Anyway you should repost this to comp.lang.c++, since you''re using C++.


他***** **** @gmail.com 写道:

我有一个应用程序,它将大量字节写入

二进制文件只是一些编组数据。


int len = Data.size(); // arrary size

for(int i = 0; i< len; ++ i)

fwrite(& Data [i],1,1,f );


现在运行了很长时间并且推动了数百万字节,一旦错过了写fData的最后一个字节,它就是
。然后进一步推动

字节再次正确。由于我没有使用

fwrite的返回值,我认为有一些错误可能是由于缓冲区

溢出在流中。这很难重现和发生在

Solaris上。

任何帮助都将不胜感激。
I have an application which writes huge number of bytes into the
binary files which is just some marshalled data.

int len = Data.size(); //arrary size
for (int i = 0; i < len; ++i)
fwrite(&Data[i], 1, 1, f);

now after running this for long time and pushing millions of bytes, It
once misses writing the last byte of fData. Then the further push of
bytes is again correct. As i am not using the return value for the
fwrite I think there is some error which could be due to buffer
overrun in the stream. This is hard to reproduce and happening on the
Solaris.
Any help will be appreciated.



它测试fwrite的返回值是个好主意!!!


为什么你不这样做而不是猜测并发布到

新闻组?

-

jacob navia

jacob at jacob point remcomp point fr

logiciels / informatique http://www.cs.virginia.edu/~lcc-win32


< he ********* @ gmail.comschrieb im Newsbeitrag

news:11 ** *******************@57g2000hsv.googlegrou ps.com ...
<he*********@gmail.comschrieb im Newsbeitrag
news:11*********************@57g2000hsv.googlegrou ps.com...

>我有一个将大量字节写入

二进制文件的应用程序,这只是一些编组数据。


int len = Data.size(); // arrary size

for(int i = 0; i< len; ++ i)

fwrite(& Data [i],1,1,f );


现在运行了很长时间并且推动了数百万字节,一旦错过了写fData的最后一个字节,它就是
。然后进一步推动

字节再次正确。由于我没有使用

fwrite的返回值,我认为有一些错误可能是由于缓冲区

溢出在流中。这很难重现并发生在

Solaris上。

任何帮助都将不胜感激。
>I have an application which writes huge number of bytes into the
binary files which is just some marshalled data.

int len = Data.size(); //arrary size
for (int i = 0; i < len; ++i)
fwrite(&Data[i], 1, 1, f);

now after running this for long time and pushing millions of bytes, It
once misses writing the last byte of fData. Then the further push of
bytes is again correct. As i am not using the return value for the
fwrite I think there is some error which could be due to buffer
overrun in the stream. This is hard to reproduce and happening on the
Solaris.
Any help will be appreciated.



您需要检查fwrite()的返回值。它不等于它''是
第三个参数,出了点问题。

你可以通过不逐字节来提高写入性能。

size_t towrite = Data.size();

size_t written = fwrite(Data,1,towrite,f);

if(written != towrite)

fputs(" Whoops!\ n",stderr);

你的代码对我来说就像C ++,所以你可能想问一下comp .lang.c ++


再见,Jojo

You''d need to check the return value of fwrite(). It it''s not equal to it''s
3rd parameter, something went wrong.
You may increase performance of your writes by not doing it byte by byte.
size_t towrite = Data.size();
size_t written = fwrite(Data, 1, towrite, f);
if (written != towrite)
fputs("Whoops!\n", stderr);
Your code looks like C++ to me, so you may want to ask in comp.lang.c++

Bye, Jojo


这篇关于fwrite()错过了写一个字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆