LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-04-2003, 10:24 AM   #1
xunilpassion
LQ Newbie
 
Registered: Oct 2003
Posts: 12

Rep: Reputation: 0
getchar() giving a hardtime


Hi all,

The getchar() function provide a hardtime for me. I am using GCC 3.2.3.

#include <stdio.h>

int main() {
int c;
c=getchar();
putchar(c);
}

I expects that as soon as i enter a character the execution should proceed with out blocking. However I am forced to hit <enter> after i input a character. Am I doing anything wrong. I don't remember, i had this problem in previous versions of gcc (I can't assertively say it though, because it is long time since i have written something in C. I rather have a bleak memory on how it was before)

The background is if you run the following snippet

#include <stdio.h>
int main() {
int c;
while((c = getchar()) != EOF)
putchar(c);
}

The while loop goes in infinite loops. There is no way to enter an EOF character (which I persume is ^Z). If i press ^Z, the execution stopes. If I hit ^C the execution breaks.

What is going on ?

~xunil
 
Old 12-04-2003, 11:22 AM   #2
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419

Rep: Reputation: 35
I think a previous post of mine can help...
http://www.linuxquestions.org/questi...hlight=getchar
 
Old 12-04-2003, 12:22 PM   #3
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
Also check out conio.h or curses.h
If either of these include files are present your compiler will support
the getch() and the getche() function -- just what you want.
 
Old 12-04-2003, 01:44 PM   #4
xunilpassion
LQ Newbie
 
Registered: Oct 2003
Posts: 12

Original Poster
Rep: Reputation: 0
Well, I got it worked. getch helped me. However, i was also having problems in getting out of the while loop. In each terminals i think the EOF character is different. CTRL-D is one working for me. My xterm is a gnome terminal. I remember using ctrl-z in dos for EOF.

Do anyone have more info on this

~xunil
 
Old 12-04-2003, 02:52 PM   #5
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419

Rep: Reputation: 35
Youre problem is not with GCC but with how the shell behaves.
If you want to be able to enter a character without having to press
enter afterwards, you are going to have to do some programming of
your own. Also, conio.h does not exist under linux, it is a borland made header. A few people have written or tried to write a conio-like library, but ncurses is still much much better.

If you want, here is my crafted Getchar function that does all the work for you:

Code:
char Getchar(void) {
char x;
//Set terminal to raw mode
system("stty raw");

//Wait for single character
x=getchar();

//Reset terminal to normal "cooked" mode
system("stty cooked");

//And we're out of here
return x;
}
here is a small example that uses it.

Code:
int main()
{
char x;
x=Getchar();
printf("%c",x);
return 0;
}
If you do use the function, dont forget to declare it first.

Last edited by jinksys; 12-04-2003 at 03:11 PM.
 
Old 12-04-2003, 04:19 PM   #6
dorian33
Member
 
Registered: Jan 2003
Location: Poland, Warsaw
Distribution: LFS, Gentoo
Posts: 591

Rep: Reputation: 32
another C code that implements DOS-like getch() function:
Code:
#include <termios.h>
#include <unistd.h>
#include <assert.h>
#include <string.h>
/*------------------------------------------------*/
unsigned char getch(void) {
 unsigned char c;
 int i;
 struct termios org_opts, new_opts;
 int res;
//-----  store old settings -----------
    res=tcgetattr(STDIN_FILENO, &org_opts);
    assert(res==0);
//---- set new terminal parms --------
    memcpy(&new_opts, &org_opts, sizeof(new_opts));
    new_opts.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ECHOPRT | ECHOKE | ICRNL);
    tcsetattr(STDIN_FILENO, TCSANOW, &new_opts);
    c=getchar();
//------  restore old settings ---------
    tcsetattr(STDIN_FILENO, TCSANOW, &org_opts);
    assert(res==0);
    return(c);
}
More coding but without extra processes generated by 'system' call.
 
Old 12-04-2003, 06:01 PM   #7
Tarts
Member
 
Registered: Feb 2003
Distribution: Slackware 9.1 (exclusively) ;)
Posts: 344

Rep: Reputation: 30
Heres a little something I made:

Code:
#include <termios.h>
#include <stdio.h>

struct termios save_att;
set_terminal(void)
{
        struct termios set_att;
        set_att.c_iflag &= ~(INLCR);
        tcgetattr(0, &save_att);
        tcsetattr(0, TCSANOW, &set_att);
}

reset_terminal(void)
{
        tcsetattr(0, TCSANOW, &save_att);
}

int main()
{
        int c;
        set_terminal();
        c = getchar();
        reset_terminal();
        putchar(c);
        return 0;
}
Tarts
{edit}

Opps, it looks like dorian33 did something simliar, or better.
With the help of the post above:

Code:
#include <termios.h>
#include <unistd.h>
#include <stdio.h>
#include <strings.h>

char charget() //I think this thread has the monopoly on 'getchar()' derivatives. 
{
        char c;
        struct termios save_att, set_att;  //learned from the post above when accessing multiable variables in a structure
        tcgetattr(0, &save_att);
        set_att.c_lflag &= ~(ICANON|ECHO|ICRNL);
        tcsetattr(0, TCSANOW, &set_att);
        c = getchar();
        tcsetattr(0, TCSANOW, &save_att);
        return c;
}

int main()
{
        int i;
        i = charget();
        putchar(i);
        return 0;
}
Tarts

Last edited by Tarts; 12-05-2003 at 08:25 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
getchar() satellite Programming 10 12-24-2017 01:47 PM
UT04 install woes (linux+anything=hardtime ) mjprater Linux - Games 1 05-29-2004 12:46 PM
about getchar() captainstorm Programming 6 10-11-2003 04:14 AM
getchar() NSKL Programming 18 01-11-2003 10:19 AM
getchar() Winter Programming 6 05-11-2002 01:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration