1st line: this is the first line of file 2nd line: this is the second 3th line: ...and this is the third 4th line: and again... 5th line: this is the line what I want to change 6th line: && now some lines at the end 7th line: seven is my favourite number 8th line: 8 is positive 9th line: the square root of 9 is integer 10th line: --- EOF ---
#include <a_samp>
public OnFilterScriptInit()
{
printf("------------------ filetest -------------");
new File: f, offset, string[ 64 ];
f = fopen("testing.txt", io_readwrite);
offset = fread(f, string); // 1st line: this is the first line of file
offset = fread(f, string); // 2nd line: this is the second
offset = fread(f, string); // 3th line: ...and this is the third
offset = fread(f, string); // 4th line: and again...
offset = fread(f, string); // 5th line: this is the line what I want to change
// now let's seek back
fseek(f, -offset, seek_current);
// okay the cursot at the beginning of the line,
// now let's overwrite it
offset = fwrite(f, "now we overwrite the FULL 5TH line of the file" );
// now let's read the other 5 lines
// WHY?!
// because, of course during scripting we don't read the line like in this script,
// && if we use fseek() & fread() in a iterate, the following results cause a infinity iterate!
offset = fread(f, string);
offset = fread(f, string);
offset = fread(f, string);
offset = fread(f, string);
offset = fread(f, string);
fclose(f);
printf("------------------ filetest -------------");
return 1;
}
1st line: this is the first line of file 2nd line: this is the second 3th line: ...and this is the third 4th line: and again... now we overwrite the FULL 5TH line of the fileline: this is the second 3th line: ...and this is the third 4th line: and again... 5th line: this is the line what I want to change 6th line: && now some lines at the end
while(( offset = fread(f, string )))
{
if( ... ) // we had to change the file's actual line
{
fseek(...);
fwrite(...);
}
}
|
If you want to seek to the beginning, simple do: fseek(file); or fseek(file, 0, seek_current); (which are actually the same) |
native fseek(File: handle, position = 0, seek_whence: whence = seek_start);
stock bool: fwrite_ex( File: handle, const string[] )
{
for( new i = 0; i < strlen( string ); i++ )
{
if( fputchar( handle, string[ i ], false ))
{
continue;
}
else
{
return false;
}
}
return true;
}
stock fread_ex( File: handle, string[] )
{
new character, value, idx = 0;
strdel( string, 0, strlen( string ));
for( ;; )
{
character = fgetchar( handle, value, false );
if( character == '\r' && fgetchar( handle, value, false ) == '\n' )
{
string[ idx ] = '\0';
idx++;
break;
}
if( character == EOF ) return 0;
string[ idx ] = character;
idx++;
}
return idx;
}