I am trying to write a C program that reads how many lines/entries there are in a set data file. I have used the code below and it works fine (gotten from: What is the easiest way to count the newlines in an ASCII file?)
#include <stdio.h>
int main()
{
FILE *correlations;
correlations = fopen("correlations.dat","r");
int c; /* Nb. int (not char) for the EOF */
unsigned long newline_count = 0;
/* count the newline characters */
while ( (c=fgetc(correlations)) != EOF ) {
if ( c == '\n' )
newline_count++;
}
printf("%lu newline characters\n", newline_count);
return 0;
}
But I was wondering if there was a way to change this bit
if ( c == '\n' )
newline_count++;
into something else so that if your data looks like
1.0
2.0
3.0
(with an entry then the new line is a space then an entry then space) instead of
1.0
2.0
3.0
How do I get it to differentiate between a character/string/integer and just a new line? I tried %s but it didn't work.. I am just trying this out first on a small file with only 3 entries, but I will be using a very big file later on where I have spaces between each line so I'm wondering how to differentiate... Or should I divide the line_count by 2 to get the number of entries?