1

If I do:

std::string buffer;

std::getline(std::cin, buffer);

Despite many niceties missing, that, say, libreadline provides, I am still able to use backspace and ENTER to enter my string. I am not sure whose responsibility it is to process these control characters. Is it the input stream std::cin, is it the underlying console device, or is it std::getline itself?

user1095108
  • 14,119
  • 9
  • 58
  • 116
  • 1
    If you're trying to control *keyboard* capabilities at the console vs. stream content, `std::getline` and, for that matter, std IO streams in-general, aren't going to get you there. You need a platform dependent solution to accomplish that. The console device is eventually what has to be controlled to prevent/allow such low-level hardware interaction. – WhozCraig Jul 12 '14 at 13:47
  • @WhozCraig I am not really trying to do anything, just wondering who it is, that processes the backspace key, as I am not getting any backspace characters back from `std::getline()`, i.e. (I don't need to go through `buffer` and process the BS control characters myself). – user1095108 Jul 12 '14 at 13:53
  • Ok, that clarifies your question considerably (i think). Makes the question more interesting as well, btw. Hard thing to describe, so props for trying to. – WhozCraig Jul 12 '14 at 14:37

2 Answers2

3

I am not sure whose responsibility it is to process these control characters. Is it the input stream std::cin, is it the underlying console device, or is it std::getline itself?

It's the console/terminal/what-have-you. If instead you feed the process a string via a pipe, you should receive, e.g. \b (backspace).

CodeClown42
  • 11,194
  • 1
  • 32
  • 67
  • So it is a blocking read from the console device, which returns once ENTER is pressed? Unless, of course, the device is redirected. – user1095108 Jul 12 '14 at 14:05
  • More or less. It's not a physical device/driver, it's an intermediate software entity such as a **[terminal](http://unix.stackexchange.com/questions/4126/what-is-the-exact-difference-between-a-terminal-a-shell-a-tty-and-a-con/4132#4132)** -- the terminology here is somewhat OS dependent, I think. If you want to take unbuffered input via a console, you need to use a library that interfaces with the terminal, such as [ncurses](http://en.wikipedia.org/wiki/Ncurses). – CodeClown42 Jul 12 '14 at 14:15
  • Also interesting WRT the *nix world: http://unix.stackexchange.com/questions/116629/how-do-keyboard-input-and-text-output-work/116630 – CodeClown42 Jul 12 '14 at 14:18
3

It's the terminal device (console).

A typical terminal will provide:

  • buffering (program receives a whole line)
  • simple line editing (at least backspace and enter, possibly line recall and cursor key editing)
  • echoing (you see what you type).

The exact features provided depend on the operating system and features set for your terminal. If you redirect standard input all those features typically disappear, and control characters are passed straight through.

david.pfx
  • 10,520
  • 3
  • 30
  • 63