C
c
hi all
i read this code from "sudoku.c", the source code for the console game
sudoku on my ubuntu box.
and i have some questions with the text i/o operations in this code.
1)
static char line[ 80 ];
static char title[ 80 ];
FILE * f; (has been initialised, i put it here for clarity)
/* Skip lines until a '%' is found */
line[ 0 ] = ' ';
while( '%' != line[ 0 ] )
if( 0 == fgets( line, sizeof( line ), f ) )
return -1;
here i am not sure of the use of fgets
does it read from file line by line ? what keeps track of which line
we will read next ?
is there a sort of internal position marker ?
2) the above code is followed by
/* Read optional title, and removing trailing whitespace */
for( p = line + 1 ; *p && isspace( *p ) ; ++p )
;
if( *p )
{
for( q = title ; '\0' != ( *q++ = *p++ ) ; )
;
--q;
while( isspace( *--q ) )
;
*++q = '\0';
}
else
strcpy( title, "(untitled)" );
i'm really confused about the pointer uses here
and how this code achieves its function, namely, removing trailing
whitespace
btw: i'm not able to judge the coding style here, whether it's the
right way to code in c. so any comments on that will be helpful too.
also can anyone point me to some well commented and well coded
opensource c program? i'm sure there's a lot out there
thanks in advance
i read this code from "sudoku.c", the source code for the console game
sudoku on my ubuntu box.
and i have some questions with the text i/o operations in this code.
1)
static char line[ 80 ];
static char title[ 80 ];
FILE * f; (has been initialised, i put it here for clarity)
/* Skip lines until a '%' is found */
line[ 0 ] = ' ';
while( '%' != line[ 0 ] )
if( 0 == fgets( line, sizeof( line ), f ) )
return -1;
here i am not sure of the use of fgets
does it read from file line by line ? what keeps track of which line
we will read next ?
is there a sort of internal position marker ?
2) the above code is followed by
/* Read optional title, and removing trailing whitespace */
for( p = line + 1 ; *p && isspace( *p ) ; ++p )
;
if( *p )
{
for( q = title ; '\0' != ( *q++ = *p++ ) ; )
;
--q;
while( isspace( *--q ) )
;
*++q = '\0';
}
else
strcpy( title, "(untitled)" );
i'm really confused about the pointer uses here
and how this code achieves its function, namely, removing trailing
whitespace
btw: i'm not able to judge the coding style here, whether it's the
right way to code in c. so any comments on that will be helpful too.
also can anyone point me to some well commented and well coded
opensource c program? i'm sure there's a lot out there
thanks in advance