AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   How to update a line in a file & file help. (https://forums.alliedmods.net/showthread.php?t=309817)

LemoNSK 08-08-2018 07:48

How to update a line in a file & file help.
 
Hello, can someone explain me why it always writes the same line in the file when I connect back? It shouldn't write after I am already registered in the file. It seems it doesn't execute ''if(equal(steamid,parsedsteamid))'' this line in the code because it always creates a new line in the file with the same SSID.

PS: Also would be glad if anyone tell me how to update my line after writing something there.

PHP Code:

    new fsize file_size(filename,1);
    for (new 
line=0;line<=fsize;line++)
    {
        
        
read_file(filename,line,readdata,127,txtlen);        
        
parse(readdata,parsedsteamid,31parsedCompletedFiveKills31);
        
get_user_authid(id,steamid,31);
        
        if(
equal(steamid,parsedsteamid))
        {
            
hasCompletedKills[id] = parsedCompletedFiveKills;
            
            break;            
        }
        else if(!
equal(steamid,parsedsteamid))
        {
            new 
writedata[128];
            new 
steamid[32], bool:completedFiveKills;
            
            
get_user_authid(id,steamid,31);
            
completedFiveKills false;
            
            
formatex(writedata,127,"%s %d",steamidcompletedFiveKills)
            
write_file(filename,writedata);
            
            break;
        }
    } 


shauli 08-08-2018 07:58

Re: How to update a line in a file & file help.
 
Log your results to a file (or use amx_log) after the 'parse' native and then look what's wrong and why your strings aren't equal.

Also, some stuff:
1. Use the new file system instead of the old one, it's much better.
2. Move the get_user_authid before the for loop starts, because now your basically calling this native each 'line' and it's the steamid never changes.
3. Remove the get_user_authid inside the 'else if' part
4. You can write 'else' instead of 'else if (!equal..', the conditions are the same.
5. When writing strings to a file (such as SteamIDs), use quotes ("%s"). it will prevent some bugs with the parse function and spaces inside the string.

klippy 08-08-2018 08:36

Re: How to update a line in a file & file help.
 
You can't update a line, you have to copy over all the text to a new temporary file with your line changed, delete the original file and rename the new file.

LemoNSK 08-08-2018 08:41

Re: How to update a line in a file & file help.
 
Quote:

Originally Posted by shauli (Post 2609073)
Log your results to a file (or use amx_log) after the 'parse' native and then look what's wrong and why your strings aren't equal.

Also, some stuff:
1. Use the new file system instead of the old one, it's much better.
2. Move the get_user_authid before the for loop starts, because now your basically calling this native each 'line' and it's the steamid never changes.
3. Remove the get_user_authid inside the 'else if' part
4. You can write 'else' instead of 'else if (!equal..', the conditions are the same.
5. When writing strings to a file (such as SteamIDs), use quotes ("%s"). it will prevent some bugs with the parse function and spaces inside the string.

Alright, one thing, tho.. It doesn't write twice anymore for me, but for any other player it writes twice, why?

LemoNSK 08-08-2018 08:42

Re: How to update a line in a file & file help.
 
Quote:

Originally Posted by KliPPy (Post 2609080)
You can't update a line, you have to copy over all the text to a new temporary file with your line changed, delete the original file and rename the new file.

wait what? Can you tell me I can't ? Could u show me example?

jimaway 08-08-2018 12:53

Re: How to update a line in a file & file help.
 
don't use break under that "else if", you will just stop the for loop from continuing when the first steamid in the file isn't the connected players one.

Code:

native write_file(const file[], const text[], line = -1);
use the third parameter to specify a line you wan't to write.


All times are GMT -4. The time now is 12:13.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.