PHP Code:
//Read first line in a file
new szLine[ 32 ] , iLen;
read_file( "test.txt" , 0 , szLine , charsmax( szLine ) , iLen );
//Overwrite first line in file
write_file( "test.txt" , "last.bat" , 0 );
You can use this to search for a line of text containing a keyword and replace it with a new line. It will return -1 if file doesnt exist, 0 if file exists but searched item was not found, 1 if searched item found and replaced.
PHP Code:
public ReplaceLineItem( const szFile[] , const szItemToFind[] , const szNewLine[] )
{
new iFilePos , szLineItem[ 512 ] , bool:bFound , iFileHandle;
if ( !( iFileHandle = fopen( szFile , "rt" ) ) )
return -1;
while ( !feof( iFileHandle ) )
{
iFilePos++;
if ( !fgets( iFileHandle , szLineItem , charsmax( szLineItem ) ) )
continue;
if( containi( szLineItem , szItemToFind ) >= 0 )
{
write_file( szFile , szNewLine , iFilePos - 1 );
bFound = true;
break;
}
}
fclose( iFileHandle );
return bFound;
}
__________________