+ QUOT; fseek的原因或fflush始终是阅读和写作之间的读/写&QUOT必需的;模式 [英] why fseek or fflush is always required between reading and writing in the read/write "+" modes

查看:145
本文介绍了+ QUOT; fseek的原因或fflush始终是阅读和写作之间的读/写&QUOT必需的;模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问:我试图更新文件
  的地方,使用的fopen模式R +,
    读取某些字符串,写
  背了修改后的字符串,但它不是
  工作。

Q: I'm trying to update a file in place, by using fopen mode "r+", reading a certain string, and writing back a modified string, but it's not working.

答:请务必之前调用fseek的
  你写的,既要寻求回
    开始你想要的字符串
  覆盖,和,因为fseek的
  或fflush始终是必需的
  读写的读/写
  +模式。

A: Be sure to call fseek before you write, both to seek back to the beginning of the string you're trying to overwrite, and because an fseek or fflush is always required between reading and writing in the read/write "+" modes.

我的问题是,为什么 fseek的 fflush 总是需要阅读和写作之间在读/写+模式? 第5.2 C陷阱与缺陷提到,这是因为向后兼容性的问题。任何一个可以详细解释一下吗?谢谢你。

My question is why fseek or fflush is always required between reading and writing in the read/write "+" modes? Section 5.2 of c traps and pitfall mentioned that it is because of backward compatibility issue. Any one can explain in details? Thanks.

推荐答案

库的缓存的输入和输出操作。退房setvbuf用来()和_IOFBF,_IOLBF参数到的Funktion。 fseek的()或fflush()的需要的库提交缓冲的操作。该标准规定了寻求或刷新操作的强制性让图书馆借了一些快捷键;否则,对于每一个I / O操作,的lib将必须检查是否previous操作也是一个读操作(或写操作),并且如果对I方向本身触发冲洗/输出改变了。

The library buffers input and output operations. Check out setvbuf() and the _IOFBF, _IOLBF parameters to that funktion. fseek() or fflush() require the library to commit buffered operations. The standard specifies a seek or flush operation as mandatory to allow the library some shortcuts; otherwise, for every I/O operation, the lib would have to check if the previous operation was also a read op (or a write op), and trigger a flush by itself if the "direction" of the I/O changed.

这篇关于+ QUOT; fseek的原因或fflush始终是阅读和写作之间的读/写&QUOT必需的;模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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