AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Writing to a File (that's already open) (https://forums.alliedmods.net/showthread.php?t=144321)

shadow.hk 12-02-2010 07:59

Writing to a File (that's already open)
 
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?

Bugsy 12-02-2010 08:53

Re: Writing to a File (that's already open)
 
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;




All times are GMT -4. The time now is 11:29.

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