Hi everyone,
I am trying to have a function that can read a file of floats.
This is very straight forward and I did that with different types of files before; however, given that previously I knew exactly the information on the last line of the file, I never bothered in making the function distinguish between an empty (but still existing) last line, and an end of file.
What I practically need to do is being able to count only the number of lines that indeed contain numbers, ruling out any additional blank line that may follow the set of numbers above.
Ex.1) File with 3 lines of numbers and ending at 1 only empty line:
Ex.2) File with 3 lines of numbers and an ARBITRARY sequence of blank lines following:
Code:
1 2
3 4
5 6
blank
...
...
blank
I usually use the following code to read a file as in ex.1, but I am unsure on how to modify it in order to stop the counting at the last numbered line although there may be many more blank lines following:
Code:
if((file_id = fopen(file_name, "r")) == NULL){
printf(" ERROR, The file %s could not be open\n \n\n", file_name);
exit(1);
}
else{
rewind(file_id); //This line resets the pointer to the beginning of the file
line_cntr = 0;
while(fgets(line, sizeof(line), file_id)!= NULL) {
line_cntr++;
*nrows = line_cntr ;
}//End WHILE
}
fclose(mesh_file_id);
}
Thank you in advance; I hope someone can give me a hint.
Best