C 中的缓冲和非缓冲输入 [英] Buffered and Unbuffered inputs in C

查看:34
本文介绍了C 中的缓冲和非缓冲输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法可以判断 inputbuffered 还是 unbuffered(手册页除外)?我们不能通过查看函数的名称来弄清楚吗?也用于 echoingnonechoing...如需快速参考,在哪里可以找到包含 BufferedUnbufferedechoingnonechoing 输入详细信息的列表?

Is there any way to figure out whether the input is buffered or unbuffered (Apart from manual pages.)? Cant we figure out by looking at the names of the functions? Also for echoing and nonechoing... For quick reference where to find the list which has the details of Buffered, Unbuffered, echoing and nonechoing inputs?

推荐答案

All stdio.h 用于读取 FILE 的函数可能会出现缓冲"或无缓冲"行为,以及回声"或非回声"行为.控制这些事情的不是您使用哪个函数,而是流和/或其底层文件描述符的设置.

All stdio.h functions for reading from a FILE may exhibit either "buffered" or "unbuffered" behavior, and either "echoing" or "non-echoing" behavior. What controls these things is not which function you use, but settings on the stream and/or its underlying file descriptor.

  • 标准库函数setvbuf 可用于启用或禁用 C 库对输入(和输出)的缓冲.这对操作系统的缓冲没有影响.存在三种可能的模式:完全缓冲"(以大量块读取或写入);行缓冲"(缓冲直到读取或写入 ' ' 字符,但不能超过该字符);和无缓冲"(所有读取和写入都立即进入操作系统).

  • The standard library function setvbuf can be used to enable or disable buffering of input (and output) by the C library. This has no effect on buffering by the operating system. There are three possible modes: "fully buffered" (read or write in substantial chunks); "line buffered" (buffer until a ' ' character is read or written, but not beyond that); and "unbuffered" (all reads and writes go to the OS immediately).

FILE 对象(包括 stdin 和朋友)的默认缓冲是实现定义的.Unixy C 库通常默认所有 FILE 为完全缓冲,但有两个例外.stderr 默认为无缓冲.对于任何其他FILE,如果在第一次实际读取或写入时没有使用setvbuf,并且isatty 对于底层文件描述符为真,然后 FILE 变为行缓冲.

The default buffering for new FILE objects (including stdin and friends) is implementation-defined. Unixy C libraries generally default all FILEs to fully buffered, with two exceptions. stderr defaults to unbuffered. For any other FILE, if setvbuf has not been used on it at the time of the first actual read or write, and isatty is true for the underlying file descriptor, then the FILE becomes line-buffered.

一些 C 库提供了扩展功能,例如__flbf 和 Linux 和 Solaris 上的朋友,供回读 一些setvbuf控制的设置.请记住,如上所述,缓冲模式可能会在第一次实际读取或写入时更改(如果尚未明确设置).

Some C libraries provide extension functions, e.g. __flbf and friends on Linux and Solaris, for reading back some of the settings controlled by setvbuf. Keep in mind that, as described above, the buffering mode can change upon the first actual read or write, if it hasn't been explicitly set.

如果输入来自文件,setvbuf 是你唯一的旋钮.如果输入来自某种通信渠道,则可能还有其他旋钮:

If input is from a file, setvbuf is the only knob you have. If input is from some sort of communication channel, there may be other knobs:

  • 在符合 POSIX 的系统上(读作除 Windows 之外的一切"),程序可以请求多种不同模式中的任何一种进行终端输入.其中,最重要的区别是在canonical"和非规范".规范模式下的终端表现出缓冲和回声.(如果没有使用 setvbuf 来禁用它,则此缓冲与 C 库可能执行的缓冲分离.)非规范模式允许您分别切换缓冲和回显.低级 POSIX 终端接口非常广泛,复杂,并允许您读取和写入所有这些设置.

  • On POSIX-conformant systems (read "everything except Windows"), a program can request any of several different modes for terminal input. Of these, the most important distinction to make is between "canonical" and "noncanonical". A terminal in canonical mode exhibits both buffering and echoing. (This buffering is separate from the buffering the C library may do if setvbuf has not been used to disable it.) Non-canonical mode allows you to toggle buffering and echoing separately. The low-level POSIX terminal interface is extensive, complicated, and allows you to both read and write all of these settings.

如果您认为要将终端置于非规范模式,那么在针对低级 POSIX API 编写一堆代码之前,您应该首先考虑 readlinencurses 库会让你的生活更轻松.

If you think you want to put the terminal in a non-canonical mode, before writing a bunch of code against the low-level POSIX API for doing so, you should first consider whether the readline or ncurses library would make your life easier.

如果您正在从管道中读取数据,那么您将受到写入管道的任何人的支配;您无法控制获得的块的大小.

If you are reading from a pipe, you are at the mercy of whoever is writing to it; you cannot control the size of the chunks you get.

如果您从套接字读取数据,您可能可以通过谨慎使用 recvmsg,但不能保证.

If you are reading from a socket, you may be able to exercise some control over the size of the chunks you get by careful use of recvmsg, but there are no guarantees.

我不知道它在 Windows 上如何.

I don't know how it is on Windows.

这篇关于C 中的缓冲和非缓冲输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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