Raised This Month: $ Target: $400
 0% 

Edit a word inside a text file


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 05-17-2015 , 04:08   Re: Edit a word inside a text file
Reply With Quote #1

You can take a look inside BombStatus plugin, it has the feature to remove specific line from file. You can adapt that and work with it.

If you are too lazy:
PHP Code:

public plugin_init()
{
    
ChangeLine("Problems""No Problems")
}

stock ChangeLine(const OldLine[], const NewLine[])
{
    new const 
FileName[] = "file_testing.ini"
    
new const TempFileName[] = "tempfile.ini"

    
new ConfigDirPath[128]; get_configsdir(ConfigDirPathcharsmax(ConfigDirPath))
    new 
FullPath[256]; formatex(FullPathcharsmax(FullPath), "%s/%s"ConfigDirPathFileName)

    new 
FilePointer fopen(FullPath"rt")
    if(
FilePointer)
    {
        new 
TempFilePath[256]; formatex(TempFilePathcharsmax(TempFilePath), "%s/%s"ConfigDirPathTempFileName)
        
        new 
InputFilePointer fopen(TempFilePath"wt")
        if(
InputFilePointer)
        {
            new 
FileData[128]
            while(!
feof(FilePointer))
            {
                
fgets(FilePointerFileDatacharsmax(FileData))
                
trim(FileData)
                
                if(
equal(FileDataOldLine))
                {
                    
copy(FileDatacharsmax(FileData), NewLine)
                }
                
fprintf(InputFilePointer"%s^n"FileData)
            }
            
            
fclose(InputFilePointer)
            
fclose(FilePointer)

            
delete_file(FullPath)
            
rename_file(TempFilePathFullPath1)
            
            return 
1
        
}
    }
    return 
0

Note that this is just an example, to show you how it could be done. Further things depend on how you will use it(if you are going to call it many times, it would be good to cache all changes that needs to be done inside an array and do it during plugin_end and adjust the string on the fly).
__________________

Last edited by HamletEagle; 05-17-2015 at 05:24.
HamletEagle 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 01:16.


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