fread() bug after using fseek()
#1

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:

Код:
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 ---
script:

Код:
#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;
}
And the result is::

Код:
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
Usually we don't reading files like in this example, we use iterates:

Код:
while(( offset = fread(f, string )))
{
    if( ... ) // we had to change the file's actual line
    {
        fseek(...);
        fwrite(...);
    }
}
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
Reply


Messages In This Thread
fread() bug after using fseek() - by [OFK]Zuckerman - 31.07.2012, 22:36
Re: fread() bug after using fseek() - by Maxips2 - 01.08.2012, 11:50
Re: fread() bug after using fseek() - by [OFK]Zuckerman - 01.08.2012, 14:22
Re: fread() bug after using fseek() - by Maxips2 - 01.08.2012, 17:11
Re: fread() bug after using fseek() - by [OFK]Zuckerman - 01.08.2012, 18:10
Re: fread() bug after using fseek() - by [OFK]Zuckerman - 01.08.2012, 19:06

Forum Jump:


Users browsing this thread: 1 Guest(s)