Portál AbcLinuxu, 25. listopadu 2026 00:12
int getche(void)
{
int vstup;
vstup=getchar();
printf("%c",vstup);
fflush(stdout);
return vstup;
}
main()
{
int a,b;
printf("zmackni dve klavesy\n");
a=getche();
b=getche();
printf("\nByly zmáčknuty tyto klavesy %c a %c\n", a, b);
}
getchar is used to read data from the keyboard ( stdin). Effectivly it is a fgetc(stdin). The problem with this function is that the characters are buffered until RETURN is hit! This means that getchar does not see the characters until the user presses return. The non standard function getche gets around this problem and sees each character as it is pressed. I have not been able to produce the same function on Linux. The nearest I can get is by using 'curses' but it keeps clearing the screen!
((((
stydím se.
Ta funkce totiz zpracovava znak bez odradkovani a muj problem spociva v tom,ze prekladac vyhlasi: undefined reference to `getche' collect2: ld returned 1 exit status.
Podle diskuse tato funkce nepracuje pod Unixy,coz mne asi zmatlo a logicky jsem predpokladal,ze existuje ekvivalent.
Jeste muze byt chyba v hlavickovych souborech a knihovnach...tedy predpokladam,pripadne mne opravte.
Kdyz clovek ztrati pul dne na takove kravine,to vetsinou k chuti neco se naucit neprida ! Opravdu je takovy problem napsat takovou funkci,ktera nacte stisknutou klavesu do promenne ? Konkretne klavesu Backspace? Tu proste pomoci getchar() neprectu i kdybych se rozkrajel
.
PS: Zatim jsem u cyklu,takze moje programatorske pokusy konci na prikazove radce
a nejake ncurses mi moc nerika.
#include <stdio.h>
#include <termios.h>
int main(int argc, char *argv[]) {
struct termios settings;
tcgetattr(0, &settings);
settings.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
settings.c_oflag &= ~OPOST;
settings.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
settings.c_cflag &= ~(CSIZE|PARENB);
settings.c_cflag |= CS8;
tcsetattr(0, TCSANOW, &settings);
while(1) {
int c=getchar(); putchar(c);
if (c=='\r') putchar('\n');
}
};
Akorát ten test na konec řádku je mrzký, měl by to taky zařizovat nějaký flag, ale teď mi to nějak nejde...
. Skoda,ze ta Knizka je takhle pro windows,ten styl se mi zamlouva. Mam taky Linux zaciname programovat,ale ta je pro zacatecnika nesrozumitelna,teda neco opisu,ale vubec netusim,proc to dela to co dela
Jeste jednou Diky !
struct termios settings;
// ahoj Leoši, kdy to přestane dělat odstavce v PRE?
tcgetattr(0, &settings);
settings.c_lflag &= ~ICANON;
settings.c_cflag |= CS8;
tcsetattr(0, TCSANOW, &settings);
Pak by se getchar() měl chovat k nerozeznání od windowsího getche() a program jde normálně ukončit ^C (ten předchozí se musel zabít z jiného terminálu).
Prostě to zatím opiš a netrap se tím ;-)
,tak jsem se konecne prihlasil
#include <termios.h>
int main(void) {
struct termios settings;
tcgetattr(0, &settings);
settings.c_lflag &= ~ICANON;
settings.c_cflag |= CS8;
tcsetattr(0, TCSANOW, &settings);
while (getchar() != 'c')
;
return 0;
}
Ja to bezmyslenkovite pastnul hned pod Include a ono to bylo MIMO funkci main().
Takze vam timto vsem dekuji za spolupraci
Proc si pripadam jako píp ??
A asi budu premyslet o necem jednodussim pro zacatek,nebo jste si tim prosli taky ?.
Ano program dela presne to same,co se od nej ocekavalo s funkci getche().
Radsi to sem pastnu !
bile_znaky.c:7: error: syntax error before numeric constant
bile_znaky.c:7: warning: data definition has no type or storage class
bile_znaky.c:8: error: syntax error before '.' token
bile_znaky.c:10: error: syntax error before numeric constant
bile_znaky.c:10: warning: data definition has no type or storage class
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.