Sunday, 15 May 2011

How can I read an unlimited string until EOF and put them a char array in C? -


For example, the user entered 3 row entries and I want to split them into one word and want to store them in four arrays.

entry:

  banana apple kiwi cherry \ n horse car blue jacket \ n Bla bla bla ^ d may contain more in the entry or fewer lines I do not know how many words are in the log I only know that the entry with the EOF ends and I All the words in the array need to be stored. Can you help me   

  #include < Stdio.h & gt; # Include & lt; Stdlib.h & gt; Int main () {char * big = malloc (256); Int c; Tall N = 0, t = 255; Printf ("Enter something too long:"); While ((c = getchar ())! = EOF) {if (n> = t) {big = realloc (big, t + 256); T + = 256; } Large [n] = c; ++ n; } Large [n] = '\ 0'; Put (big); // because it is faster than printf // or printf ("% s", bigger); Free (big); Return 0; }  

This works for me


No comments:

Post a Comment