Finding line size of each row in a text file
c
Solution
You can iterate through each character in the line and keep incrementing a counter until the end-of-line (`'\n'`) is encountered. Make sure to open the file in text mode (`"r"`) and not binary mode (`"rb"`). Otherwise the stream won't automatically convert different platforms' line ending sequences into `'\n'` characters.
Here is an example:
int charcount( FILE *const fin )
{
int c, count;
count = 0;
for( ;; )
{
c = fgetc( fin );
if( c == EOF || c == '\n' )
break;
++count;
}
return count;
}
Here's an example program to test the above function:
#include <stdio.h>
int main( int argc, char **argv )
{
FILE *fin;
fin = fopen( "test.txt", "r" );
if( fin == NULL )
return 1;
printf( "Character count: %d.\n", charcount( fin ) );
fclose( fin );
return 0;
}
Problem
How can you count the number of characters or numbers in each line? Is there something like a EOF thats more like a End of Line?