Raised This Month: $ Target: $400
 0% 

How to update a line in a file & file help.


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
LemoNSK
Member
Join Date: Jul 2018
Location: Criminal Underworld
Old 08-08-2018 , 07:48   How to update a line in a file & file help.
Reply With Quote #1

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;
        }
    } 
LemoNSK is offline
shauli
Member
Join Date: Jun 2018
Old 08-08-2018 , 07:58   Re: How to update a line in a file & file help.
Reply With Quote #2

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.

Last edited by shauli; 08-08-2018 at 07:59.
shauli is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 08-08-2018 , 08:36   Re: How to update a line in a file & file help.
Reply With Quote #3

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.
__________________
klippy is offline
LemoNSK
Member
Join Date: Jul 2018
Location: Criminal Underworld
Old 08-08-2018 , 08:41   Re: How to update a line in a file & file help.
Reply With Quote #4

Quote:
Originally Posted by shauli View Post
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 is offline
LemoNSK
Member
Join Date: Jul 2018
Location: Criminal Underworld
Old 08-08-2018 , 08:42   Re: How to update a line in a file & file help.
Reply With Quote #5

Quote:
Originally Posted by KliPPy View Post
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?
LemoNSK is offline
jimaway
Heeeere's Jimmy!
Join Date: Jan 2009
Location: Estonia
Old 08-08-2018 , 12:53   Re: How to update a line in a file & file help.
Reply With Quote #6

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.
jimaway is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 20:44.


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