I write a matrix in a text file as it shows the example:
2 5 2 5 6
2 3 4 6 3
2 5 6 8 6
I want to find out the number of lines and columns. I've started writing the code but I can't determine the number of lines.
Code:
#include <stdio.h>
int main()
{
FILE *f;
f = fopen ("mat1.txt", "r");
int spaces=0, endl=0;
char c, d;
do {
fscanf(f, "%c", &c);
if ( c == ' ' )
spaces++;
}
while ( c != '\n' );
fseek ( f, 0, SEEK_SET );
do {
fscanf(f, "%c", &d);
if ( d == '\n' )
endl++;
}
while (!EOF);
printf("The matrix has %d columns and %d lines.", spaces+1, endl+1);
fclose(f);
return 0;
}
What did I do wrong ?