cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
Beginners
read file
read file
Mar 10, 2013 at 1:46pm UTC
Dirk23
(1)
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
Mar 10, 2013 at 1:47pm UTC
Mar 10, 2013 at 3:44pm UTC
mariostg
(35)
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.