read file

I want to read a file, but this code works only for the first line. What should I do if I want it too work for the second and third and .. line also?

char* read_file(const char* filename) {
FILE *file;
char *lijn;
file = fopen(filename,"r");
if(file==NULL){
printf("Fout bij het openen van %s\n\n", filename);
}
lijn = (char*)malloc(get_file_length(file)*sizeof(char*));
fgets(lijn,get_file_length(file),file);
printf("Ingelezen lijn: %s\n\n", lijn);
fclose(file);
return lijn;
}
Last edited on
You should look at this example http://www.cplusplus.com/reference/cstdio/fgets/
fgets reads up a given number of characters or \n, which ever comes first. To read an entire file, you would want to use a loop.
Topic archived. No new replies allowed.