c - How do I read in a string pass the newline character? -
i have read in file such as
apple grape banana
and store string, fgets reads newline , stops, reading in apple.
how around this? or how can store 3 words separate strings?
char* readfile(const char *filename) { file *infile; infile=fopen(filename, "r"); char **stringinfile; stringinfile = malloc(sizeof(char*)*50); char *data = fgets(stringinfile,50,infile); printf("%s", data); fclose(infile); return data; }
this in c btw.
fgets()
reading one line each call, , sets file courser next line. if want read file, have iterate it. check if @ end, can check eof flag feof()
. resulting in, me working:
char* readfile(const char *filename) { file *infile; infile=fopen(filename, "r"); char **stringinfile; stringinfile = malloc(sizeof(char*)*50); while(!feof(infile)) { printf("%s", fgets(stringinfile,50,infile)); } fclose(infile); return stringinfile; }
and, don't need variable data
- fgets() first parameter character array, automatical stored(for example apple
in programm).
Comments
Post a Comment