By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,060 Members | 1,906 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,060 IT Pros & Developers. It's quick & easy.

Getch() in Linux

P: 2
Does anyone know of a getch() equivalent that exists in linux?
I have just started doing some basic programming and am currently running Knoppix on CD.
So I'm not sure what kind of file library I have available.

Any help would be greatly appreciated;

GL
Jun 21 '06 #1
Share this Question
Share on Google+
3 Replies


Banfa
Expert Mod 5K+
P: 8,916
What programming language are you talking about? C/C++?

In my MSVC I can find

getc
getchar
_getch

getc and getch are standard library functions and should be found in any C/C++ implementation.

_getch is a platform implementation function (denoted by the _ at the start of the name), a function provide by the platform in question to support the rest of the c standard library.

The closest standard library equivilent to _getch is getchar.
Jun 21 '06 #2

P: 1
What programming language are you talking about? C/C++?

In my MSVC I can find

getc
getchar
_getch

getc and getch are standard library functions and should be found in any C/C++ implementation.

_getch is a platform implementation function (denoted by the _ at the start of the name), a function provide by the platform in question to support the rest of the c standard library.

The closest standard library equivilent to _getch is getchar.

Put the following text in to Gedit and save as getch.h
to use the function use mygetch()

works like a charm
Expand|Select|Wrap|Line Numbers
  1. /
  2.  
  3. #include <termios.h>
  4. #include <unistd.h>
  5.  
  6. int mygetch(void)
  7. {
  8. struct termios oldt,
  9. newt;
  10. int ch;
  11. tcgetattr( STDIN_FILENO, &oldt );
  12. newt = oldt;
  13. newt.c_lflag &= ~( ICANON | ECHO );
  14. tcsetattr( STDIN_FILENO, TCSANOW, &newt );
  15. ch = getchar();
  16. tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
  17. return ch;
  18. }
  19.  
/solution provided by kermi3 from this web posting http://cboard.cprogramming.com/archi...p/t-27714.html
Oct 1 '08 #3

P: 1
@justaguy1010


Actually the compiler says that getchar() "is not declared in this scope".

I found out that you have to include the <cstdio> library.

Also, it appears this function can't read the '\r' character. (The enter key).

Anyway, thank you so much! you helped me a lot!
May 17 '19 #4

Post your reply

Sign in to post your reply or Sign up for a free account.