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
#2

If you want to seek to the beginning, simple do:
fseek(file);
or fseek(file, 0, seek_current);
(which are actually the same)

I have noticed that fseek is always returning 0 altough it should return the new pos...
Reply
#3

Quote:

If you want to seek to the beginning, simple do:
fseek(file);
or fseek(file, 0, seek_current);
(which are actually the same)

Are you sure? The whence parameter have the default value seek_start.

Код:
native fseek(File: handle, position = 0, seek_whence: whence = seek_start);
But the problem is not seeking to the beginning.

Zuckerman
Reply
#4

Sorry I meant: fseek(file, 0, seek_start);
And what mode are you using? it only works with io_read/io_readwrite/ftemp
Reply
#5

Only io_readwrite.

Use only io_read maybe would be a good idea, but I'm need to change the file :S
Reply
#6

In the last one hour I tried to reading & writing files with fputchar() & fgetchar(), maybe with these functions this problem won't appear.

Код:
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;
}
The results: EXACTLY THE SAME bugged result.

Now I absolutely no have idea. Maybe I will try with fblockread() & fblockwrite() but I'm not expecting any surprise.


Zuckerman
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)