1

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..

Thanks.

user884685
  • 47
  • 2
  • 4
  • 8

3 Answers3

4

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;
  tcgetattr(0,&old);
  new = old;
  new.c_lflag&=~ICANON;
  tcsetattr(0,TCSANOW,&new);
  i = 0;
  while (read(0,&c,1) && c!='\n' && i < 80) r[i++] = c;
  r[i] = 0;
  tcsetattr(0,TCSANOW,&old);
  printf("Entered <%s>\n",r);
  return 0;
}
pizza
  • 7,296
  • 1
  • 25
  • 22
0
#include<stdio.h>
...
int count=0;
char buffer[81];
int ch=getchar();
while(count<80&&ch!='\n'&&ch!='\r'&&ch!=EOF){
    buffer[count]=ch;
    count=count+1;
    ch=getchar();
}
buffer[count]='\0';

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 */
Ariel
  • 404
  • 2
  • 4
  • `getchar()` returns an **int**, you goose. Otherwise EOF won't work :-) – paxdiablo Apr 04 '12 at 04:39
  • **char**-typed variables in C are **int**-s, but I might as well make the code correct :) – Ariel Apr 04 '12 at 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 Apr 04 '12 at 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 Apr 04 '12 at 04:48
  • Code fixed. Not bad for it being the first C code I've written in about a year and a half. – Ariel Apr 04 '12 at 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 Apr 04 '12 at 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 Apr 04 '12 at 05:05
0
#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';
        break;
    }
    buf[i] = c;
}
buf[sizeof(buf] - 1] = '\0';
Sean
  • 5,290
  • 2
  • 24
  • 21
  • 1
    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 Apr 04 '12 at 04:46