How is std::iostream buffered?

后端 未结 2 818
臣服心动
臣服心动 2020-12-31 01:12

General use case

I am trying to implement a basic shell.

Description

I need to read user input until some delimiters are pressed so a correspondi

相关标签:
2条回答
  • 2020-12-31 01:36

    If you are on a POSIX operating system, you can set the terminal to be unbuffered using the functions and structures declared in termios.h. Basically you need to disable canonical input, and setup the terminal for non-canonical mode. These are some links that can help you with understanding the difference between the two terminal modes:

    1. Noncanonical Input (from the libc manual)

      In non-canonical input mode, the special editing characters such as ERASE and KILL are ignored. The system facilities for the user to edit input are disabled in noncanonical mode, so that all input characters (unless they are special for signal or flow-control purposes) are passed to the application program exactly as typed. It is up to the application program to give the user ways to edit the input, if appropriate.

    2. Canonical vs. non-canonical terminal input

      For canonical input - think shell; actually, think good old-fashioned Bourne shell, since Bash and relatives have command-line editing. You type a line of input; if you make a mistake, you use the erase character (default is backspace, usually; sometimes DEL) to erase the previous character ... For non-canonical input - think vi or vim or ... you press a character, and it is immediately available to the program. You aren't held up until you hit return.

    3. Description of Terminal Interface

      This chapter describes a general terminal interface that is provided to control asynchronous communications ports. It is implementation-dependent whether it supports network connections or synchronous ports or both.

    In essence though, the issue you're encountering is not with the C++ iostream interface itself, but rather has to-do with how the controlling terminal that the C++ iostream interface is reading from has been setup. Thus taking advantage of unbuffered I/O is going to be a platform-dependent operation, and will differ dependending on whether you're using Windows, or an actual POSIX-compliant platform (this includes POSIX-environments for Windows such as Cygwin).

    If you find that messing around with the terminal settings is too much of a problem, you can also look into a cross-platform curses programming library such as PDCurses that will abstract most of the complexities of the underlying terminal types.

    0 讨论(0)
  • 2020-12-31 01:45

    The answers to the immediate questions on whether and how std::istream is buffered are: yes, std::istream is buffer using a class derived from std::streambuf which defines the actual buffering and reading approach for a concrete source (or, when using an std::ostream for a destination). Whether this really does any buffering depends on this concrete class and its operation can generally not avoided.

    That said , this isn't you problem! The problem is that normally input isn't sent to standard input if a program until the newline key is hit. This is so that some line editing can be done by the terminal implementation and doesn't have to be done by every program. Unfortunately, there is no portable approach to change this. On POSIX youcan turn the standard input stream (using file descriptor 0) into non-canonical mode using tcgetattr() and tcsetattr(). I don't know how to achieve this on non-POSIX systems.

    0 讨论(0)
提交回复
热议问题