MyTetra Share
Делитесь знаниями!
Время создания: 10.12.2008 12:46
Текстовые метки: C, C++, Linux
Раздел: Компьютер - Программирование - Язык C (Си)
Запись: xintrea/mytetra_syncro/master/base/0000000367/text.html на raw.github.com

getch() не входит в стандарт C, в линуксе ты его не найдешь. В винде эта функция есть в conio.h. Остальные извесные функции такие как getchar, cin требуют подтверждения ввода (нажатия Enter). Попробуй такой вот код:

#include <stdio.h>

#ifndef __linux

#include <conio.h>

#else

#include <unistd.h>

#include <termios.h>

int getch()

{

int ch;

struct termios oldt, newt;

tcgetattr( STDIN_FILENO, &oldt );

newt = oldt;

newt.c_lflag &= ~( ICANON | ECHO );

tcsetattr( STDIN_FILENO, TCSANOW, &newt );

ch = getchar();

tcsetattr( STDIN_FILENO, TCSANOW, &oldt );

return ch;

}

#endif

int main(int argc, char *argv[])

{

int ch;

while (ch !=27) {

ch = getch();

printf("Key %c code %d\n", ch, ch);

}

return 0;

}

Еще вариант:

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <termios.h>

int main()

{

char c;

struct termios tty, savetty;

if (!isatty(0)) // Если stdin - не терминал, то работать смысла нет

{

printf("stdin is not a tty!\n");

return -1;

}

printf("Answer y or n: ");

fflush(stdout); // вывели буфер

tcgetattr(0, &tty); // получили структуру termios

savetty = tty; // сохранили

tty.c_lflag &= ~(ISIG | ICANON);

// ISIG - when any of the characters INTR, QUIT, SUSP, or DSUSP are received, generate the corresponding signal.

// ICANON - enable canonical mode. This enables the special characters EOF, EOL, EOL2, ERASE, KILL, REPRINT, STATUS, and WERASE, and buffers by lines.

tty.c_cc[VMIN] = 1;

tcsetattr(0, TCSAFLUSH, &tty);

read(0, &c, 1);

tcsetattr(0, TCSANOW, &savetty);

if ( c != 'y' && c != 'n')

{

printf("\nInvalid reply.\n");

}

else

{

printf("\n%s\n", c== 'y'?"Yes!":"No!");

}

return 0;

}

Так же в этом разделе:
 
MyTetra Share v.0.59
Яндекс индекс цитирования