fseek() problem
#1

Hi! I have a very simply question.

I have a file, and I search for a string in it. If i find the string, I'd like to change that file's line what is including this string.

Код:
new File: f;
f = fopen( "test1.txt" );
new temp[ 128 ];
while( fread( f, temp ))
{
    if( strfind( temp, "string what i search for", false ) != -1 )
    {
        // Now i know that here is the line what I searched for.
        // The problem is that, the position in the file is the NEXT line
        fseek( f, 0, seek_current );
        fwrite( f, "here is the string what I want to take to the line" );
    }
}
fclose(f);
The problem is that tha changings are in the NEXT line, not in that line where I found my searched string. How can I fix it? Thank you very much.

Zuckerman
Reply
#2

I assume that fread sets the file cursor to the end of the line when it's read, so that's why it only writes into the next line. However, fread also returns the number of characters read, so with that in mind you should be able to determine the position to write.

pawn Код:
new File: f;
f = fopen( "test1.txt" );
new temp[ 128 ];
new offset; // add this here
while((offset = fread( f, temp ))) // Double brackets to avoid unintended assignment warning
{
    if( strfind( temp, "string what i search for", false ) != -1 )
    {
        // Now i know that here is the line what I searched for.
        // The problem is that, the position in the file is the NEXT line
        fseek( f, -offset, seek_current );
        fwrite( f, "here is the string what I want to take to the line" );
    }
}
fclose(f);
Reply
#3

THANK YOU very much

https://sampwiki.blast.hk/wiki/Fread

Yes, now I can see that the info about the returning value is changed 2 days ago. Thanks,

Zuckerman
Reply
#4

Okay, I'm working on a file-controller system for my gamemode, and now I tested it. It's not works correctly.

test1.txt ORIGINAL text before running the code:

Код:
1. line: test1
2. line: second
3. line: pawn
4. line: scripting
5. line: forum
6. line: next line is the topic
7. line: so, string what i search for blablabla
8. line: plus 8th line
9. line: plus 9th line
10. line: the very last
11. EOF
filterscript:

Код:
#include <a_samp>


public OnFilterScriptInit()
{
	new File: f;
	f = fopen( "test1.txt" );
	new temp[ 128 ];
	new offset; // add this here
	while((offset = fread( f, temp ))) // Double brackets to avoid unintended assignment warning
	{
	    if( strfind( temp, "string what i search for", false ) != -1 )
	    {
	        // Now i know that here is the line what I searched for.
	        // The problem is that, the position in the file is the NEXT line
	        fseek( f, -offset, seek_current );
	        fwrite( f, "here is the string what I want to take to the line" );
	    }
	}
	fclose(f);
}
Result of running is a infinity iteration. I stopped the samp-server.exe, and the test1.txt file after running:

Код:
1. line: test1
2. line: second
3. line: pawn
4. line: scripting
5. line: forum
6. line: next line is the topic
here is the string what I want to take to the line line: scripting
5. line: forum
6. line: next line is the topic
here is the string what I want to take to the line line: scripting
5. line: forum
6. line: next line is the topic
here is the string what I want to take to the line line: scripting
5. line: forum
6. line: next line is the topic
here is the string what I want to take to the line line: scripting
5. line: forum
6. line: next line is the topic
here is the string what I want to take to the line line: scripting
5. line: forum
6. line: next line is the topic
here is the string what I want to take to the line line: scripting
5. line: forum
6. line: next line is the topic
here is the string what I want to take to the line line: scripting
5. line: forum
6. line: next line is the topic
here is the string what I want to take to the line line: scripting
5. line: forum
6. line: next line is the topic
here is the string what I want to take to the line line: scripting

The file is full of with this three lines on 180000 lines (& I stopped the .exe!)

Any idea? Beacuse I absolutely don't know why this is the result. Thank you very much,


Zuckerman
Reply
#5

bump - sorry but I'm really need this :S
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)