I am practicing programming in C and I decided to create a function that will count the amount of rows in a file for later use in creating a matrix. The problem comes in when I provided it with different file types. I noticed that when I provide the function with a txt file it counts one less then it needs, while a csv is counting the correct amount of rows.
int countRows(char fileName[100]){
FILE *fp;
int nl = 1;
char c;
fp = fopen(fileName, "r");
for (c = getc(fp); c != EOF; c = getc(fp)){
if(c == '\n'){
nl = nl + 1;
}
}
fclose(fp);
return nl;
}
Say if I were to have a txt file as
age name score
15 jared 90
16 jerome 85
18 timmy 9
I would expect an output of 4 rows.