String Input

Write here if you have problems with your C source code

Moderator: Board moderators

Post Reply
TUHIN
New poster
Posts: 2
Joined: Fri May 06, 2005 10:56 pm
Location: RUET, BANGLADESH

String Input

Post 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
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post 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?
jakabjr
Learning poster
Posts: 56
Joined: Wed Mar 23, 2005 9:21 pm
Location: Timisoara, Romania

Post 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) ==
Understanding a problem in a natural way will lead to a natural solution
Post Reply

Return to “C”