Raised This Month: $12 Target: $400
 3% 

Writing to a File (that's already open)


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
shadow.hk
Senior Member
Join Date: Dec 2008
Location: WA, Australia
Old 12-02-2010 , 07:59   Writing to a File (that's already open)
Reply With Quote #1

Would it be possible to edit a file while it's open in-code. Like so...

PHP Code:
/**
 * Edits a map in the maps file.
 * 
 * @param mapname        Map to change
 * @param difficulty    Difficulty to change to
 * 
 * @return                True on success, false on failure
 */
bool:EditMap( const mapname[ ], difficulty )
{
    new 
szLine64 ], szMapName32 ], szMapDifficulty], iLinebSuccess;
    
    new 
iFile fopenmapsfile"a+" );
    
    if( 
iFile )
    {
        while( !
feofiFile ) )
        {
            
fgetsiFileszLinesizeofszLine ) - );
            
trimszLine );
            
            if( !
szLine] || szLine] == '^n' || szLine] == ';' || ( szLine] == '/' && szLine] == '/' ) )
            {
                
iLine++;
                continue;
            }
            
            
parseszLineszMapNamesizeofszMapName ) - 1szMapDifficultysizeofszMapDifficulty ) - );
            
            
// found the map
            
if( equalszMapNamemapname ) )
            {
                new 
szTempString32 ];
                
formatexszTempStringsizeofszTempString ) - 1"%s %i"mapnamedifficulty );
                
                if( 
write_filemapsfileszTempStringiLine ) )
                {
                    
bSuccess true;
                }
                
                break;
            }
            
            
iLine++;
        }
        
        
fcloseiFile );
    }
    
    return 
bSuccess;

Or would I have to close it, then write to file, then re-open it again and use fseek to reset the current line?
shadow.hk is offline
Send a message via MSN to shadow.hk
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 12-02-2010 , 08:53   Re: Writing to a File (that's already open)
Reply With Quote #2

You shouldn't have any problems doing it like that, I've used a similar method for removing admins from users.ini. Link

If you want to be on the safe side just write to the file after it has been closed.

PHP Code:
bool:EditMap( const mapname[ ], difficulty )
{
    new 
iFile szLine64 ], szMapName32 ], szMapDifficulty], iLinebool:bSuccess bool:bFound;
    
    if( ( 
iFile fopenmapsfile "a+" ) ) )
    {
        while( !
feofiFile ) )
        {
            
fgetsiFileszLinecharsmaxszLine ) );
            
trimszLine );
            
            if( !
szLine] || szLine] == '^n' || szLine] == ';' || ( szLine] == '/' && szLine] == '/' ) )
            {
                
iLine++;
                continue;
            }
            
            
parseszLineszMapNamesizeofszMapName ) - 1szMapDifficultysizeofszMapDifficulty ) - );
            
            
// found the map
            
if( equalszMapName mapname ) )
            {
                
bFound true;
                break;
            }
            
            
iLine++;
        }
        
        
fcloseiFile );
    }
    
    if ( 
bFound )
    {
        
formatexszMapNamecharsmaxszMapName ) , "%s %i" mapname difficulty );
                
        
bSuccess bool:write_filemapsfileszMapNameiLine );
    }    
                
    return 
bSuccess;

__________________
Bugsy 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 03:16.


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