fgets() returns the number of characters read in the line, when you reach the end of the file it will output 0. feof() is still the proper choice, even though using fgets() as the condition technically could still work.
Code:
// native fgets(file, buffer[], maxlength);
static cell AMX_NATIVE_CALL amx_fgets(AMX *amx, cell *params)
{
FileObject* fp = reinterpret_cast<FileObject*>(params[1]);
if (!fp)
{
return 0;
}
static char buffer[4096];
buffer[0] = '\0';
fp->ReadLine(buffer, sizeof(buffer) - 1);
return set_amxstring_utf8(amx, params[2], buffer, strlen(buffer), params[3]);
}
Using a file named "test.txt" with the below contents:
it gives the below output (notice it will read the new line character)
Code:
6 chars = AAAAA
6 chars = BBBBB
5 chars = CCCCC
EOF reached
PHP Code:
new iFile = fopen( "test.txt" , "rt" );
new szTest[ 10 ] , iChars;
while ( ( iChars = fgets( iFile , szTest , charsmax( szTest ) ) ) )
{
server_print( "%d chars = %s" , iChars , szTest);
}
fclose( iFile );
server_print( "EOF reached" );
__________________