为什么输出后面不能跟输入,反之亦然? [英] Why can't an output be followed by an input and vice versa?

查看:107
本文介绍了为什么输出后面不能跟输入,反之亦然?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自APUE

打开文件进行读写时(加号 类型),则有两个限制.

When a file is opened for reading and writing (the plus sign in the type), two restrictions apply.

•在没有输入的情况下,输出不能直接跟在输入后面 插入fflush,fseek,fsetpos或倒带.

• Output cannot be directly followed by input without an intervening fflush, fseek, fsetpos, or rewind.

•如果没有输入,输入之后不能直接跟随输出 介入fseek,fsetpos或倒带,或输入操作 遇到文件的结尾.

• Input cannot be directly followed by output without an intervening fseek, fsetpos, or rewind, or an input operation that encounters an end of file.

为什么在两种情况下都"fseek,fsetpos或倒带"?

Why "fseek, fsetpos, or rewind" in both cases?

为什么在第二种情况下输入操作遇到文件末尾"?

Why "an input operation that encounters an end of file" in the second case?

谢谢.

Linux API的类似问题是可以在read()之后直接跟着write()和在write()之后跟着read()吗?

A similar question for Linux API is Can `read()` be directly followed by `write()` and `write()` by `read()`?

推荐答案

在使用FILE流时,在读取或写入文件时会使用一个内部缓冲区.

When working with a FILE stream, there is a single internal buffer used when either reading from or writing to the file.

在读写之间切换时,必须先 清除该缓冲区,然后再切换模式,否则有可能发生数据丢失.上面提到的每个操作都会执行所需的缓冲区刷新.

When switching between reading and writing, that buffer must be cleared before switching modes, otherwise data loss could potentially occur. Each of the operations mentioned above perform the required flushing of the buffer.

这篇关于为什么输出后面不能跟输入,反之亦然?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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