Raised This Month: $ Target: $400
 0% 

[ SOLVED ] Overwriting line in file


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
xPaw
Retired AMX Mod X Moderator
Join Date: Jul 2008
Old 09-26-2009 , 07:55   [ SOLVED ] Overwriting line in file
Reply With Quote #1

Ok, so i have some info in file format like

mapname origin[ 0 ] origin[ 1 ] origin[ 2 ] .....

i checked if mapname already exists in the file and tryed to overwrite it, BUT it overwrite 1 line down, not the line wich i wanted, any ideas?

PHP Code:
            new bool:bFoundszData32 ], iFile fopeng_szFile"r+" );
            
            if( !
iFile )
                return 
PLUGIN_HANDLED;
            
            while( !
feofiFile ) ) {
                
fgetsiFileszData31 );
                
parseszDataszData31 );
                
                if( 
equalszDatag_szMapname ) ) {
                    
bFound true;
                    
log_amx"A line with this map already exists!" );
                    
                    
fseekiFile0SEEK_CUR );
                    
                    
fprintfiFile"%s %f %f %f %f %f %f^n"g_szMapname,
                    
vOrigin][ ], vOrigin][ ], vOrigin][ ],
                    
vOrigin][ ], vOrigin][ ], vOrigin][ ] );
                    
                    break;
                }
            } 
__________________

Last edited by xPaw; 09-26-2009 at 10:47.
xPaw is offline
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 09-26-2009 , 08:21   Re: Overwriting line in file
Reply With Quote #2

Quote:
Originally Posted by xPaw View Post
Ok, so i have some info in file format like

mapname origin[ 0 ] origin[ 1 ] origin[ 2 ] .....

i checked if mapname already exists in the file and tryed to overwrite it, BUT it overwrite 1 line down, not the line wich i wanted, any ideas?

PHP Code:
            new bool:bFoundszData32 ], iFile fopeng_szFile"r+" );
            
            if( !
iFile )
                return 
PLUGIN_HANDLED;
            
            while( !
feofiFile ) ) {
                
fgetsiFileszData31 );
                
parseszDataszData31 );
                
                if( 
equalszDatag_szMapname ) ) {
                    
bFound true;
                    
log_amx"A line with this map already exists!" );
                    
                    
fseekiFile0SEEK_CUR );
                    
                    
fprintfiFile"%s %f %f %f %f %f %f^n"g_szMapname,
                    
vOrigin][ ], vOrigin][ ], vOrigin][ ],
                    
vOrigin][ ], vOrigin][ ], vOrigin][ ] );
                    
                    break;
                }
            } 
The problem is that at the time you use fseek, fgets has already parsed that line and moved the file pointer to the next one. Probably you will have to avoid fgets. But, beware that probably you don't want to do this. You can't insert bytes in the middle of the file so the only way of this working right is if the map name has always the same characters (having spaces when it doesn't fill the entity space) and the same to the floats.
__________________
joaquimandrade is offline
xPaw
Retired AMX Mod X Moderator
Join Date: Jul 2008
Old 09-26-2009 , 08:31   Re: Overwriting line in file
Reply With Quote #3

Hum maybe fseek before fgets ? So the write pointer will be on the current ? O.o
__________________
xPaw is offline
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 09-26-2009 , 08:34   Re: Overwriting line in file
Reply With Quote #4

Quote:
Originally Posted by xPaw View Post
Hum maybe fseek before fgets ? So the write pointer will be on the current ? O.o
I never did this but try SEEK_CUR-1

Edit: or

PHP Code:
fseek(file, -1SEEK_CUR);  
fseek(file0SEEK_CUR); 
__________________

Last edited by joaquimandrade; 09-26-2009 at 08:37.
joaquimandrade is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 09-26-2009 , 09:48   Re: Overwriting line in file
Reply With Quote #5

This function will remove an admin from users.ini (basically will just affix a semi-colon in front) but you can tweak this to totally overwrite the line with new data.

PHP Code:
public cmd_RemoveAdmin(szRemoveAuthID[])
{
    new 
szAuthID[35];
    new 
bool:bUserFound false;
    new 
iFilePos;
    new 
szUsersFile[64];
    new 
szLineItem[512];
    
    
get_localinfo("amxx_configsdir" szUsersFile 63);
    
format(szUsersFile,63,"%s/users.ini",szUsersFile);
    
    new 
iFileHandle fopen(szUsersFile,"rt");
    
iFilePos 0;

    while(!
feof(iFileHandle))
    {
        
fgets(iFileHandle,szLineItem,511);

        
iFilePos++;

        if(
szLineItem[0] == ';')
            continue;

        
parse(szLineItem,szAuthID,34);
        
        if(  
equal(szAuthIDszRemoveAuthID)   )
        {
            
format(szLineItem,511,";%s",szLineItem);
            
write_file(szUsersFile,szLineItemiFilePos 1);
            
bUserFound true;
            
            break;
        }
    }
    
    if( !
bUserFound )
        
log_amx("Admin [%s] was not found to remove from users.ini." szRemoveAuthID );

    
fclose(iFileHandle);

__________________

Last edited by Bugsy; 09-26-2009 at 09:58.
Bugsy is offline
Old 09-26-2009, 09:52
Bugsy
This message has been deleted by Bugsy. Reason: doublepost
xPaw
Retired AMX Mod X Moderator
Join Date: Jul 2008
Old 09-26-2009 , 09:54   Re: Overwriting line in file
Reply With Quote #6

Haha lol just counting and then write_file, good solution, thank you ;)
__________________
xPaw is offline
AntiBots
Veteran Member
Join Date: May 2008
Location: Brazil
Old 09-26-2009 , 10:15   Re: Overwriting line in file
Reply With Quote #7

From mi KzArg:
Also Write v_angle

PHP Code:
stock kz_set_start_end(Float:fOrigin[6], bool:mode true)
{
    static 
RealFile[192], TempFile[192], fnflenFormatfOrigin[128]
    
kz_get_configsdir(RealFile191)
    
add(RealFile191g_sStartEndFile); len copy(TempFile191RealFile)
    
formatex(TempFile[len], 191-len"_temp.txt")
    
    
formatex(FormatfOrigin127"%f, %f, %f, %f, %f, %f"fOrigin[0], fOrigin[1], fOrigin[2], fOrigin[3], fOrigin[4], fOrigin[5])

    if( 
mode )
        
g_fDefaultStartOrigin fOrigin
    
else
        
g_fDefaultEndOrigin fOrigin
    
    f 
fopen(RealFile"rt")
    
nf fopen(TempFile"wt")
    
    static 
StrData[192], StrKey[64]
    static 
bool:replacedreplaced false
    
    
while( !feof(f) )
    {
        
fgets(fStrData192)
        
parse(StrData[2], StrKey63)
        
        if( ( 
mode == true StrData[0] == 'S' StrData[0] == 'E' ) && equali(StrKeyg_sMapName) && !replaced )
        {
            
fprintf(nf"%s %s = %s^n"mode == true "S" "E"g_sMapNameFormatfOrigin)
            
            
replaced true
        
}
        else
        {
            
fputs(nfStrData)
        }
    }
    
    if( !
replaced )
    {
        
fprintf(nf"%s %s = %s^n"mode == true "S" "E"g_sMapNameFormatfOrigin)
    }
    
    
fclose(f); fclose(nf)
    
    
delete_file(RealFile)
    while( !
rename_file(TempFileRealFile1) ) { }

__________________
AntiBots is offline
Send a message via ICQ to AntiBots Send a message via MSN to AntiBots Send a message via Skype™ to AntiBots
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 09-26-2009 , 10:25   Re: Overwriting line in file
Reply With Quote #8

Quote:
Originally Posted by AntiBots View Post
From mi KzArg:
Also Write v_angle

PHP Code:
stock kz_set_start_end(Float:fOrigin[6], bool:mode true)
{
    static 
RealFile[192], TempFile[192], fnflenFormatfOrigin[128]
    
kz_get_configsdir(RealFile191)
    
add(RealFile191g_sStartEndFile); len copy(TempFile191RealFile)
    
formatex(TempFile[len], 191-len"_temp.txt")
    
    
formatex(FormatfOrigin127"%f, %f, %f, %f, %f, %f"fOrigin[0], fOrigin[1], fOrigin[2], fOrigin[3], fOrigin[4], fOrigin[5])

    if( 
mode )
        
g_fDefaultStartOrigin fOrigin
    
else
        
g_fDefaultEndOrigin fOrigin
    
    f 
fopen(RealFile"rt")
    
nf fopen(TempFile"wt")
    
    static 
StrData[192], StrKey[64]
    static 
bool:replacedreplaced false
    
    
while( !feof(f) )
    {
        
fgets(fStrData192)
        
parse(StrData[2], StrKey63)
        
        if( ( 
mode == true StrData[0] == 'S' StrData[0] == 'E' ) && equali(StrKeyg_sMapName) && !replaced )
        {
            
fprintf(nf"%s %s = %s^n"mode == true "S" "E"g_sMapNameFormatfOrigin)
            
            
replaced true
        
}
        else
        {
            
fputs(nfStrData)
        }
    }
    
    if( !
replaced )
    {
        
fprintf(nf"%s %s = %s^n"mode == true "S" "E"g_sMapNameFormatfOrigin)
    }
    
    
fclose(f); fclose(nf)
    
    
delete_file(RealFile)
    while( !
rename_file(TempFileRealFile1) ) { }

He wants to use one file only and do inline replacing.

About Bugsy solution, I think that you still need to make lines larger enough to hold the biggest length possible. In the case described it works because it is writing only two chars. Also, write_file parses the file from the beginning so, in a big file it reads from the first char until that line so if you can make it with fseek it will be better performing.

Edit: it isn't writing two chars but still test first if you can write more data than what you have in the line
__________________

Last edited by joaquimandrade; 09-26-2009 at 10:27.
joaquimandrade is offline
xPaw
Retired AMX Mod X Moderator
Join Date: Jul 2008
Old 09-26-2009 , 10:48   Re: [ SOLVED ] Overwriting line in file
Reply With Quote #9

@AntiBots: Do i care about your kzarg ?
@joaquimandrade: Tested and it works perfectly
__________________
xPaw is offline
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 09-26-2009 , 10:57   Re: [ SOLVED ] Overwriting line in file
Reply With Quote #10

Quote:
Originally Posted by xPaw View Post
@AntiBots: Do i care about your kzarg ?
@joaquimandrade: Tested and it works perfectly
And you can increase a line (that is not the last one)'s length?
__________________
joaquimandrade 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 07:06.


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