c - Allocating Memory for a whole line of input of variable length stored as a single string -


i'm learning c , reading around best way take in input line line.

basically want create strings hold whole line of input. question below doesn't answer how should handle memory allocation string.

how can scan strings spaces in them using scanf()?

should first detect how space needed? how? should realloc every time need more space?

please show example

input file or stdin(both possible not @ same time)

the following repeatedly allocates buffer storage until input has been read (malloc/realloc checks omitted):

char *mygetline(file *in) {     char *buffer= null;     int c, i, j;      i= 0; j= 0;         {         c= fgetc(in);         if (i==j) {             j += 1024;             buffer= realloc(buffer, j);         }         if (c==eof) break;         buffer[i++]= c;     } while (1);     buffer[i]= '\0';     return buffer; } 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -