Archived

This topic is now archived and is closed to further replies.

LonelyStar

Funcion, which takes one (and only one) character from stdin and returns

Recommended Posts

LonelyStar    192
Hi, I am looking for an function which takes one (and only one) character from stdin and returns. if I do
#include <stdio.h>

int main()
{
  unsigned char c;
    {
      c=fgetc(stdin);
      fputc(c,stdout);
    }
  return 0;
}
I can type like how long I want until I press return. But I only want to get the first character, examine it and then decide if I want to get more characters. Example output: dfggegrgsfvd d Thanks [edited by - LonelyStar on March 23, 2004 7:21:24 AM]

Share this post


Link to post
Share on other sites
Bagpuss    122
Excuse me from asking this but I am confused. Do you want to read in a single character from stdin, and then based on that you may want to read in more ?
To just get a single character from stdin, why not use getch() (or if you still need to echo to the screen, use getche())

If you are looking for an extended key (cursors, shift, alt etc) then you need to call it twice, the first time it will return 0xe0.

This is all in the msdn help online, and on the CD''s (assuming you have access to them)

Bp

Share this post


Link to post
Share on other sites
LonelyStar    192
Thanks Bagpuss for your reply.
It was a little stupid of me not to mention that I am on a Linux machine. So getch() is excactly what I am looking for but it does not exist under Unix.
I found a solution, anybody who has the same problem and understands German: http://www.ohse.de/uwe/articles/getch.html

Sorry that my question was so inaccurate and thanks for your reply, Bagpuss!!!

Share this post


Link to post
Share on other sites
BlueDev    126
Hello LonelyStar,

I think I know of an easy way to accomplish this:

#include <stdio.h>

int main() {
unsigned char c;
char buffer[1];

// Get information

fgets(buffer, 2, stdin);

// Send first byte to character

c = buffer[0];

// Only send first character of buffer

fputc(c, stdout);

return 0;
}
Example 1.1: Retrieving data and sending only one byte to stdout.

Not sure if this is what you are looking for, but I get the inputted string with a max of 2 characters, 0 and 1, and the I send the first byte of buffer, "buffer[0]", to stdout.

If this was partially what you were looking for, or something different please let me know.


Hope this helps,
[BDS]StackOverflow

Share this post


Link to post
Share on other sites
LonelyStar    192
Thanks BlueDev for your reply but it was not what I was looking for.

The thing is, I want the user only to be able to input somthing in a special formating and I want to test the formating while the user is typing.

For example, if I want the user to type a number and he starts by typing ''g'' I do not want the ''g'' to appear on the screen, I want nothing to happen. But, on the other hand, if he types an ''8'' the ''8'' should appear on the screen at once.

Numbers are just an simple example of an formating. Another would be a date.

Share this post


Link to post
Share on other sites
George2    187
getch does exist on linux (type man getch into a terminal and you will see)

you need to include curses.h and probably add -lcurses to your linker options

Share this post


Link to post
Share on other sites
cilcoder    122
Yeah, listen to George2, checkout ncurses. There is nothing in the standard C/C++ that will do what you want. I read somewhere that the C++(stdin, etc..) doesn''t get the input until the user presses return so it can''t possibly be able to do what you want.

Share this post


Link to post
Share on other sites
SiCrane    11839
BlueDev: your code has a buffer overrun error. fgets() will append a null terminating character to the end of the input read, which will overflow your buffer.

Share this post


Link to post
Share on other sites
amag    152
You can write getch() on Linux, you just need to "man tcgetattr" and "man tcsetattr" (at least I think these exists on Linux). (Uhm, and of course without the ")
Otherwise ioctl() might do the job for you as well...

[edited by - amag on March 23, 2004 6:29:26 PM]

Share this post


Link to post
Share on other sites