I had installed ncurses library in Linux mint and still I can't use getch function in c. I am using Linux mint 18.2.
Here is my program:
#include <stdio.h>
#include <curses.h>
int main() {
char k;
printf("how are you");
k = getch();
printf("%c",k);
}
and here is the output:
ram#ram$ gcc-7 test.c -lcurses
ram#ram$ ./a.out
how are you�ram#ram$
It does't wait for me to press any key and terminate to quickly. I don't want to install conio.h for Linux. How can I use getch and getche function in Linux? Please don't tell me to make my own function. I am still a noob. Or there must be alternatives.
Here's a "corrected" version, explaining what's wrong in the comments:
#include <curses.h>
#include <stdio.h>
int main(void)
{
// use the correct type, see https://linux.die.net/man/3/getch
int k;
// init curses:
initscr();
// in curses, you have to use curses functions for all terminal I/O
addstr("How are you?");
k = getch();
// end curses:
endwin();
printf("You entered %c\n", k);
return 0;
}
This still isn't good code, you should at least check whether you got a valid character from getch().
It's also important to note that getch() isn't a "function of C". It's part of curses, a well-known platform-independent API for console/terminal control, with implementations e.g. for *nix systems (ncurses) and Windows (pdcurses). It's not part of the language C.
Related
In the next code: I don't have to press Enter to get the character with getch() and this is only applicable on Windows (mingw) . I am programming a simple stopwatch which reacts if a keyboard-key is pressed without the need to press ENTER, but the same thing doesn't work on Linux ( GCC ). And I have to find a solution only using getch() with no echo. I've been Googling around with no luck. Thank you in advance.
PS: I am a c/c++ beginner.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void) {
printf("\n\t\tStopwatch \n\n \t Press S to start \n\n");
time_t start;
char c ;
struct tm tm ;
do {
c=getch(); /*get the Character without pressing ENTER*/
if (c!= 'S' && c!= 's') printf("\nWrong key. Please press'S' to Start\n");
} while(c!= 'S' && c!= 's');
start=time(NULL);
tm = *localtime(&start);
printf("\n Starting time :: %d:%d:%d \n", tm.tm_hour, tm.tm_min, tm.tm_sec);
return 0;
}
Unix (and so Linux) have a concept of terminal (TTY) which can be pretty complicated.
Every process can be associated with a terminal (and lot actually are).
This terminal has different options like the ECHO you mentioned and the LINE BUFFER (which is the enter problem which you reported).
To set a terminal the low level APIs are termios (see "man termios").
Another more friendly API is ncurses (see "man ncurses"). For instance at https://www.mkssoftware.com/docs/man3/curs_inopts.3.asp you can see some function for different settings.
An easy example with ncurses (you can compile with "gcc -O2 source.c -o output -lncurses"):
#include <stdio.h>
#include <ncurses.h>
int main(void)
{
initscr();
cbreak();
noecho();
int n = getch();
printf("%d %c\n", n, n);
}
I am working on Ubuntu these days. When I compiled my C program using gcc, it is giving the error conio.h doesn't exists.
I want to use clrscr() and getch() function.
Can you please tell me the substitute of this header file in linux.
The getch() function can be found in curses.h (library "curses"). The same library offers functions to clear the screen. Check out these links:
http://linux.die.net/man/3/getch
http://linux.die.net/man/3/erase
system("clear"); can be used in linux instead of clrscr();
# include <curses.h>
int erase(void);
int werase(WINDOW *win);
int clear(void);
int wclear(WINDOW *win);
int clrtobot(void);
int wclrtobot(WINDOW *win);
int clrtoeol(void);
int wclrtoeol(WINDOW *win);
DESCRIPTION
The erase and werase routines copy blanks to every position in
the window, clearing the screen.
I'm guessing this question was repeatedly downvoted because it implies a poor understanding of basic C language features and/or that OP is simply copying/pasting code into editor/IDE.
Similarly, just use system("exit"); within your code:
#include<stdlib.h>
main()
{
system("clear"); //clears the screen
}
Checking the man-pages shows:
SYSTEM(3) Linux Programmer's Manual SYSTEM(3)
NAME
system - execute a shell command
SYNOPSIS
#include <stdlib.h>
int system(const char *command);
DESCRIPTION
system() executes a command specified in command by calling /bin/sh -c
command, and returns after the command has been completed.
During execution of the command, SIGCHLD will be blocked, and SIGINT
and SIGQUIT will be ignored.
It could also be the case that this question is a possible duplicate of the following:
How to implement getch() function of C in Linux?
Why can't I find <conio.h> on Linux?
GNU/Linux replacements for Turbo C functions `clrscr` and `cprintf`
Function clrscr in C and C++
"UNDEFINED REFRENCE TO clrscr();"
Substitute for getch(), gotoxy(), delay(), clrscr()
What is Equivalent to getch() & getche() in Linux?
Finally, take a look at the following for more details and examples:
http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/scanw.html#GETCHCLASS
http://ubuntuforums.org/showthread.php?t=549023
Apparently you didn't try googling.
There are no direct alternatives.
This blog post: http://wesley.vidiqatch.org/code-snippets/alternative-for-getch-and-getche-on-linux/ provides you with alternatives for getch() and getche()
Alternatively you can use libncurses to do what you want: http://tech.dir.groups.yahoo.com/group/linux/message/29221
curses.h is an alternative for conio.h.
install build-essentials and install libncurses5-dev.
Then you can work with that functions.
[http://ubuntuforums.org/showthread.php?t=880601][1]
I was tinkering around with some codes; after i installed ncurses, I inserted these codes:
#include <stdio.h>
#include <ncurses.h>
main ()
{
system ("clear");
getchar ();
}
There is another way to do it through C code instead system call.
void clrscr(void) {
fprintf(stdout, "\033[2J\033[0;0f");
fflush(stdout);
}
I found it long time ago and I've checked it on raspbian successfully.
And also:
void gotoxy(int x, int y) {
printf("%c[%d;%df",0x1B, y, x);
}
I hope it helps you.
Regards.
In G++ Compiler, we use system("clear") function defined in stdlib.h header File
#include<iostream>
#include<stdlib.h>
int main() {
std::cout<<"Hello Aliens:";
system("clear");
}
I use windows 8, and Code::Block 13.12.
I have code C language like that:
#include <stdio.h>
#include <conio.h>
main()
{
while (!kbhit())
printf("You haven't pressed a key.\n");
return 0;
}
When I press a key, It not Stop, i don't think my code wrong, have any suggest for me to fix this?
And this is my Screen: http://i.imgur.com/8Rsj8J7.png
Update: I tested on some another computer, It work! (same code)
From kbhit()
Explanation: This function is not defined as part of the ANSI C/C++ standard. It is generally used by Borland's family of compilers. It returns a non-zero integer if a key is in the keyboard buffer. It will not wait for a key to be pressed.
I am working on Ubuntu these days. When I compiled my C program using gcc, it is giving the error conio.h doesn't exists.
I want to use clrscr() and getch() function.
Can you please tell me the substitute of this header file in linux.
The getch() function can be found in curses.h (library "curses"). The same library offers functions to clear the screen. Check out these links:
http://linux.die.net/man/3/getch
http://linux.die.net/man/3/erase
system("clear"); can be used in linux instead of clrscr();
# include <curses.h>
int erase(void);
int werase(WINDOW *win);
int clear(void);
int wclear(WINDOW *win);
int clrtobot(void);
int wclrtobot(WINDOW *win);
int clrtoeol(void);
int wclrtoeol(WINDOW *win);
DESCRIPTION
The erase and werase routines copy blanks to every position in
the window, clearing the screen.
I'm guessing this question was repeatedly downvoted because it implies a poor understanding of basic C language features and/or that OP is simply copying/pasting code into editor/IDE.
Similarly, just use system("exit"); within your code:
#include<stdlib.h>
main()
{
system("clear"); //clears the screen
}
Checking the man-pages shows:
SYSTEM(3) Linux Programmer's Manual SYSTEM(3)
NAME
system - execute a shell command
SYNOPSIS
#include <stdlib.h>
int system(const char *command);
DESCRIPTION
system() executes a command specified in command by calling /bin/sh -c
command, and returns after the command has been completed.
During execution of the command, SIGCHLD will be blocked, and SIGINT
and SIGQUIT will be ignored.
It could also be the case that this question is a possible duplicate of the following:
How to implement getch() function of C in Linux?
Why can't I find <conio.h> on Linux?
GNU/Linux replacements for Turbo C functions `clrscr` and `cprintf`
Function clrscr in C and C++
"UNDEFINED REFRENCE TO clrscr();"
Substitute for getch(), gotoxy(), delay(), clrscr()
What is Equivalent to getch() & getche() in Linux?
Finally, take a look at the following for more details and examples:
http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/scanw.html#GETCHCLASS
http://ubuntuforums.org/showthread.php?t=549023
Apparently you didn't try googling.
There are no direct alternatives.
This blog post: http://wesley.vidiqatch.org/code-snippets/alternative-for-getch-and-getche-on-linux/ provides you with alternatives for getch() and getche()
Alternatively you can use libncurses to do what you want: http://tech.dir.groups.yahoo.com/group/linux/message/29221
curses.h is an alternative for conio.h.
install build-essentials and install libncurses5-dev.
Then you can work with that functions.
[http://ubuntuforums.org/showthread.php?t=880601][1]
I was tinkering around with some codes; after i installed ncurses, I inserted these codes:
#include <stdio.h>
#include <ncurses.h>
main ()
{
system ("clear");
getchar ();
}
There is another way to do it through C code instead system call.
void clrscr(void) {
fprintf(stdout, "\033[2J\033[0;0f");
fflush(stdout);
}
I found it long time ago and I've checked it on raspbian successfully.
And also:
void gotoxy(int x, int y) {
printf("%c[%d;%df",0x1B, y, x);
}
I hope it helps you.
Regards.
In G++ Compiler, we use system("clear") function defined in stdlib.h header File
#include<iostream>
#include<stdlib.h>
int main() {
std::cout<<"Hello Aliens:";
system("clear");
}
Today , When i coding, met a question..my Code as follow:
#include<stdlib.h>
void main()
{
system("dir");
getch();
}
The question : The user Screen is nothing..Why ? where is my result?
If you want the output when using system, at least into something you can read in your application, you need to pipe the output:
system("dir > /tmp/output.txt");
FILE *f = fopen("/tmp/output.txt", "r");
char text[1024]; // max sizeof of 1 kb, any more and I'd consider using `malloc()` instead.
fread(text, 1, 1024, f);
printf("%s\n", text);
fclose(f);
There are some problems in your program, at least one of which has already been mentioned.
void main() should be int main(void).
As I recall, the Windows/DOS getch function is declared in <conio.h>; you should have a #include directive for it. Be aware that both <conio.h> and getch are non-standard.
Since main returns int, you should return an int result.
But none of these problems explain the problem you're seeing.
With these changes:
#include <stdlib.h>
#include <conio.h>
int main(void)
{
system("dir");
getch();
return 0;
}
This should work; it should show a directory listing of whatever directory your program runs in (which is determined by TC; I don't know the details).
It's possible that the program is running in an empty directory, which means the dir command wouldn't show any files, but it should still produce some output.
Try commenting out the system() call and adding a printf call (note the added #include <stdio.h>):
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main(void)
{
printf("Hello, world\n");
getch();
return 0;
}
This should open a console window, print "Hello, world" in it, and wait for you to type Enter.
If you still don't see any output (either no console window, or a console window with nothing in it), then you have a problem that's not related to the system() call. Most likely the problem has to do with the way you're using Turbo C (I presume that's what "TC" stands for).
The main function in every C program is supposed to return an int you are returning void
Change void to int:
#include<stdlib.h>
int main()
{
system("dir");
getch();
}
When I tested, the dir command ran in my console and printed to standard out.
May be he is the running the program directly in the Turbo C IDE and hence his output is not visible. If he runs the program directly from cmd line it works. I remember you need to run Alt - F5 or some other combination to see the output window in Turbo C++