31.07.2012, 22:36
Hi!
I'm suffering with this all the last three days. If I use fseek() - everything is okay. But if I use the fread() function after this, some "rubbish" string is copied into the file.
Here is the original testing.txt file in scriptfiles folder:
script:
And the result is::
Usually we don't reading files like in this example, we use iterates:
But using fread() after fseek() && fread() is cause infinity iterate & the "rubbish" strings are written into the files until I stop the samp-server.exe.
I'm really can't imagine why this is. If this is my fault I apologize, but then, what is the problem?
Zuckerman
I'm suffering with this all the last three days. If I use fseek() - everything is okay. But if I use the fread() function after this, some "rubbish" string is copied into the file.
Here is the original testing.txt file in scriptfiles folder:
Код:
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(...); } }
I'm really can't imagine why this is. If this is my fault I apologize, but then, what is the problem?
Zuckerman