C - Reading from stdin as characters are typed

Go To StackoverFlow.com


How do I fill an 80-character buffer with characters as they are being entered or until the carriage return key is pressed, or the buffer is full, whichever occurs first.

I've looked into a lot of different ways, but enter has to be pressed then the input char* gets cut off at 80..


2012-04-04 04:23
by user884685
look into curses, or ioct - Dave 2012-04-04 04:24
will do thanks - user884685 2012-04-04 04:26
How can I find the header files for ioct - user884685 2012-04-04 04:46
See http://stackoverflow.com/questions/4023895/how-to-read-string-entered-by-user-in-c/4023921#4023921 for a robust solution for user input - paxdiablo 2012-04-04 04:48
paxdiablo has the right idea. The solution he referenced uses fgets() which seems to do what you want - Sean 2012-04-04 04:59


If you really want the characters "as they are entered", you cannot use C io. You have to do it the unix way. (or windows way)

#include <stdio.h>
#include <unistd.h>
#include <termios.h>
int main() {
  char r[81];
  int i;
  struct termios old,new;
  char c;
  new = old;
  i = 0;
  while (read(0,&c,1) && c!='\n' && i < 80) r[i++] = c;
  r[i] = 0;
  printf("Entered <%s>\n",r);
  return 0;
2012-04-04 05:42
by pizza
Thanks, I'll see how this works. - user884685 2012-04-08 23:57


int count=0;
char buffer[81];
int ch=getchar();

Once you have buffer as a string, make sure you digest the rest of the line of input to get the input stream ready for its next use.

This can be done by the following code (taken from the scanf section of this document):

scanf("%*[^\n]");   /* Skip to the End of the Line */
scanf("%*1[\n]");   /* Skip One Newline */
2012-04-04 04:35
by Ariel
getchar() returns an int, you goose. Otherwise EOF won't work :- - paxdiablo 2012-04-04 04:39
char-typed variables in C are int-s, but I might as well make the code correct : - Ariel 2012-04-04 04:41
chars are not ints, they may be integral types but that's irrelevant. The reason getchar returns an int is because you have to be able to return every single char plus an eof indicator - paxdiablo 2012-04-04 04:43
I don't think you need the scanf() stuff. If you've read until the end of the line or until EOF then you don't have any more characters in the input stream, do you - Sean 2012-04-04 04:48
Code fixed. Not bad for it being the first C code I've written in about a year and a half - Ariel 2012-04-04 04:48
@Sean , Not at the moment of the user input, but if the program is going to do more input later, there needs to be code to make sure that any remnant data in the input buffer for this part is purged before reading for other data - Ariel 2012-04-04 04:51
Ariel - Doesn't the return/enter key still need to be pressed to get the input (I'm thinking of the case when 80+ chars are read - user884685 2012-04-04 05:05


#include <stdio>
char buf[80];
int i;
for (i = 0; i < sizeof(buf) - 1; i++)
    int c = getchar();
    if ( (c == '\n') || (c == EOF) )
        buf[i] = '\0';
    buf[i] = c;
buf[sizeof(buf] - 1] = '\0';
2012-04-04 04:41
by Sean
You have a bit of a problem at the end. Let's say the first key you press is ENTER, you will break from the loop and put the null terminator at position 1 rather than 0. In other words, there'll be a rubbish character in your string - paxdiablo 2012-04-04 04:46
Good point! This should work a little better - Sean 2012-04-04 04:52