Page 1 of 1

String Input

Posted: Tue May 24, 2005 7:47 pm
by TUHIN
For many purposes, i need to take a large input of string.
I have declared a character array in large size. But i cannot take
more than 128 characters input as string from keyboard.
Can anyone help me about htis problem ?


-TUHIN

Posted: Tue May 24, 2005 8:24 pm
by Krzysztof Duleba
Why don't you provide a simple test case to show how do you exactly fail to read more than 128 bytes?

You could also read some manual, but I'm sure you have already done it. You wouldn't ask before reading it, would you?

Posted: Thu May 26, 2005 10:39 am
by jakabjr
u can try this for reading a line of unlimited size, with dynamic allocation:

Code: Select all

#include <stdio.h>
#include <stdlib.h>
const int BLOCK = 16;

char *getline(void) {
  char *p, *s = NULL;
  int c, lim = -1, size = 0;
  while ((c = getchar()) != EOF) {
     if (size >= lim)
       if (!(p = realloc(s, (lim+=BLOCK)+1))) {
         ungetc(c, stdin);
         break;
       } else s = p;
       if ((s[size++] = c) ==