I am newbie and have a C - Linux program that read a number of variable lines like this:
12 34 56 78
3 4 7 900
...
%d %d %d %d
...
1 2 3 4
The function I've written so far is like this:
void Import(const char* path_of_file)
{
FILE *cfile = fopen(path_of_file, "r");
int lines = 0;
//I copied it from another question
while (EOF != (fscanf(cfile,"%*[^/n]"), fscanf(cfile, "%*c"))) ++lines;
fclose(cfile);
cfile = fopen(path_of_file, "w");
...
for( i=0; i<n; i++)
{
...
fscanf(cfile, "%d %d %d %d", &a, &b, &c, &d);
...
}
...
//Of course it will return something, but it shouldn't matter here
fclose(cfile);
}
But may be because the text file is created in Windows or some thing, it doesn't always end with a newline, and so my program frequently miss the last line.
Is there anyway to automatic append a newline at the end of file if it's not there? Or better, is there any way to read the last line without that newline? A example function based on the one above will be very good.