Originally Posted by
rac1
fgets only takes the first line, you dont know how many lines are there in the text so i think you should write your own fgets function to do better operations.
1) fgets() reads the file one line at a time... got 25000 lines... use a loop.
2) Nobody reads files character by character... that's just slow and silly
3) If the file is already open why on earth would you close it and re-open it,
4) If you did #3, why wouldn't you check if it actually opened?
5) if you want to know how big a file is...
Code:
fseek(file,0,SEEK_END);
size = ftell(file);
fseek(file,0,SEEK_SET);
6) In c you don't cast the return value of malloc()... if your compiler complains it's in C++ mode.
7) Your routine is horridly inefficient since it has to read the file twice... character by character, no less.
8) if you simply want to read the whole file into memory...
Code:
char *buffer = malloc(size);
fread(buffer,1,size,file);