S
sieg1974
Hi,
The function bellow returns the number of lines of a file.
The length of the char variable __Line is 256, and I think it would
allow me to store strings with up to 255 letters because the 256th
character would always be "0".
My question is why there is a segmentation fault when fseek tries to
read 255 characters from the file if the length of __Line is 256? And
it works fine when it reads up to 254 characters?
Thanks a lot,
Andre
int getNumberLines( const char * _fileName )
{
FILE * __File;
char __Line[ 256 ];
int __NumberLines = 0;
if( ( __File = fopen( _fileName, "r" ) ) == NULL )
return( -1 );
while( fgets( __Line, 254, __File ) )
__NumberLines++;
fclose ( __File );
return( __NumberLines );
}
The function bellow returns the number of lines of a file.
The length of the char variable __Line is 256, and I think it would
allow me to store strings with up to 255 letters because the 256th
character would always be "0".
My question is why there is a segmentation fault when fseek tries to
read 255 characters from the file if the length of __Line is 256? And
it works fine when it reads up to 254 characters?
Thanks a lot,
Andre
int getNumberLines( const char * _fileName )
{
FILE * __File;
char __Line[ 256 ];
int __NumberLines = 0;
if( ( __File = fopen( _fileName, "r" ) ) == NULL )
return( -1 );
while( fgets( __Line, 254, __File ) )
__NumberLines++;
fclose ( __File );
return( __NumberLines );
}