[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [CPROG] key pressed (was: c++ og standard libraries



Christian Treldal <sslug@sslug> writes:

>> men den er pokkers besværlig at bruge, så jeg har lavet et eksempel:
>> http://www.sslug.dk/~chlor/keypressed.c

> Det var lige det magiske buzzword. Det og c++ googlede mig lige frem til
> det eksempel jeg lige har brug for-
>
> /* getchar example : typewriter */
> #include <stdio.h>
>
> int main ()
> {
>   char c;
>   puts ("Enter text. Include a dot ('.') in a sentence to exit:");
>   do {
>     c=getchar();
>     putchar (c);
>   } while (c != '.');
>   return 0;
> }
>
> Samme princip som dit eksempel bare lidt mere overskueligt

Den gør dog ikke det samme. getchar() indlæser eet tegn fra stdin, men
UNIX terminaler starter i "cooked mode" som giver dig en *linie* ad
gangen. Det er nødvendigt med al det kode som Hans har skrevet for at
kunne modtage data med det samme og være nogenlunde ligesom Borland's
kbhit.

Desuden har programmet en fejl: getchar() returner typen "int" og ikke
"char" fordi getchar() kan enten returnere et normalt tegn eller EOF. 
Man skal derfor lægge værdien af getchar() i en int og sammenligne den
med EOF. Ovenstående program gør det ikke og vil gå amok hvis du
trykker control-D for at lukke stdin, eller fx kører program <
/dev/null.

Generelt er det ikke tilrådeligt at skifte fra cooked mode for at
spørge brugere om et eller andet. Fortæl ham heller kan skal trykke
Y/N og RETURN. Hvis du absolut skal håndtere tastetryk med det samme,
så brug ncurses biblioteket.



-- 
===============================================================
<sslug@sslug>                           Herlev, Denmark     
<URL:http://www.andreasen.org/>                             <*>   
===============================================================



 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 20:09 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *