6

I want to make interactive console application, which allows to enter command in loop. For example, user types "search" and program finds some data and prints it on the screen. Then the program waits for the next command (which can be search, quit or other). For user's convenience I want my program supports command history (like in terminal, when pressing up and down arrow on the keyboard). But I can't realize how to do that because I don't know how to print text which can be read further by scanf, std::getline, std::cin and so on. So code std::cin << "hello"; isn't compiled (no match for ‘operator<<’ in ‘std::cin << "hello"’). Function fprintf(stdin, "hello"); prints nothing and scanf can't read this printed message. It is evident that std::getline(std::cin, str); and scanf("%s", s); and gets(s) and so on can't read text which has been outputted by printf or std::out. So the question is: how can I print text on console which also will be in stdin (std::cin)? Or maybe there is more elegant way to organize command history?

P.S. I also thought about simulating of button pressing to print the text I need, but I hope there is better way to make command history

P.P.S. I use Linux and C++

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
user2717575
  • 369
  • 7
  • 16
  • It helps when you remember that standard I/O started on systemw without a screen, but with a printer. `printf` literally printed text. To read that back with `scanf` would have required a human operator feeding the actual paper output to a scanner. – MSalters Aug 29 '13 at 09:08

2 Answers2

3

Use the readline and history libraries, which are made exactly for that purpose.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • Note that the GNU versions of these require all code linked to them to be licensed under the GPL. – Zan Lynx Aug 19 '14 at 23:20
  • That link is now dead. Can you update your answer, or provide detailed library names – TSG Jul 12 '22 at 00:55
1

If you don't want to use a library like those Kerrek SB suggested, you might think in another direction:

1) What commands should be in the history? -> All the commands, the user typed. 2) How do you know, what the user typed? -> You get it from std::in 3) What do you do with the commands, you get from std::in? -> You process them (e.g. start a search when the user typed 'search')

Additionally to step 3 you can just store the commands a user typed internally (e.g. in some kind of vector). If now your user wants to use the command history and presses 'key up' (or 'key down') you just look up the corresponding command in your internal vector. He presses 'enter' afterwards? Just process the command, the user selected from your internal command-history.

sebi
  • 431
  • 1
  • 4
  • 9
  • I thought about it, but the user might want to change command before pressing 'enter'. For example he has made a mistake in the command and wants to correct it and start a fixed command. Therefore I can't only trace 'key up' and 'enter' buttons. – user2717575 Aug 29 '13 at 09:04