- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
// https://www.tutorialspoint.com/Read-a-character-from-standard-input-without-waiting-for-a-newline-in-Cplusplus
// A portable solution doesn't exist for doing this. On windows, you can use the getch() function from the conio(Console I/O) library to get characters pressed. For example,
#include<iostream>
#include<conio.h>
using namespace std;
int main() {
char c;
while(1){ // infinite loop
c = getch();
cout << c;
}
}
// This will output whatever character you input to the terminal. Note that this will only work on windows as the conio library exists only on windows. On UNIX, you can achieve this by entering in system raw mode. For example,
#include<iostream>
#include<stdio.h>
int main() {
char c;
// Set the terminal to raw mode
system("stty raw");
while(1) {
c = getchar();
// terminate when "." is pressed
if(c == '.') {
system("stty cooked");
exit(0);
}
std::cout << c << " was pressed."<< std::endl;
}
}
Т.е. с отрубленной построчной буферизацией "std::cin >>" в винде не заюзать, я правильно понимаю?
Например, https://ru.cppreference.com/w/c/thread
Или вот в новых стандартах крестов есть какая-то хренота для работы с файловой системой в стандартной библиотеке https://en.cppreference.com/w/cpp/filesystem
В общем это не аргумент.
Х.з., может быть у венды тоже какой-то флажок есть. Гуглить надо.
вообще странное желание конечно читать напрямую, когла есть readline и этот.. как его.. аналог не гнутый... забыл
http://www.delorie.com/gnu/docs/readline/rlman_41.html
Функция rl_callback_read_char читает символ, но нам его не возвращает. Она вызывает предварительно установленный нами обработчик, если заметила признак конца строки. Т. е. наш обработчик сможет прочитать только целиком строку, завершающуюся ентером.
В общем, можно текстовый «Тетрис» соорудить.
http://rus-linux.net/MyLDP/BOOKS/programming-ground-up/02/groundup-ru-02-07-03.html
Как видим, всё очень просто.