AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved [HELP] Replace a line from .ini (https://forums.alliedmods.net/showthread.php?t=306901)

CrAzY MaN 04-18-2018 03:39

[HELP] Replace a line from .ini
 
Is there any method to replace a whole line from a .ini file?

I want to search for a string in that line, and replace it.

DarthMan 04-18-2018 04:03

Re: [HELP] Replace a line from .ini
 
Quote:

Originally Posted by CrAzY MaN (Post 2588277)
Is there any method to replace a whole line from a .ini file?

I want to search for a string in that line, and replace it.

The easiest thing will be to use dynamic arrays, add every line to the array, excluding the one you want to modify. For that 1, just put the replaced line on the array, close the file that was read, then write every array to the file using the a+ flag. You would have to do a loop like for(new i = 0; i < ArraySize(ArrayName); i++) and then ArrayGetString and put the string on the file.

Natsheh 04-18-2018 04:06

Re: [HELP] Replace a line from .ini
 
Yes, create a new file copy all the data from the old file to the new file, except that line you want to replace, remove the oldfile rename the new file name to the oldfile name or simply just use write_file( const file[], const data[], line=the line position you want to replace )

1st method is the solution for Cant write file access problem for some hosts.

DarthMan 04-18-2018 04:16

Re: [HELP] Replace a line from .ini
 
Quote:

Originally Posted by Natsheh (Post 2588281)
Yes, create a new file copy all the data from the old file to the new file, except that line you want to replace, remove the oldfile rename the new file name to the oldfile name or simply just use write_file( const file[], const data[], line=the line position you want to replace )

1st method is the solution for Cant write file access problem for some hosts.

Using array and the newer file system with fopen, fputs & fclose is the better way to go.

Natsheh 04-18-2018 05:56

Re: [HELP] Replace a line from .ini
 
Quote:

Originally Posted by DarthMan (Post 2588283)
Using array and the newer file system with fopen, fputs & fclose is the better way to go.

Probably you didn't understand a thing from what i have said.

Bugsy 04-18-2018 07:14

Re: [HELP] Replace a line from .ini
 
This has been asked many times, I'm sure you can find the code to do this on the forum. I think I've written this myself multiple times

DarthMan 04-18-2018 08:48

Re: [HELP] Replace a line from .ini
 
Quote:

Originally Posted by Natsheh (Post 2588290)
Probably you didn't understand a thing from what i have said.

Well, I did I was jsut pointing to your write_file, explaining that it's better to use the method with fputs.

Natsheh 04-18-2018 09:12

Re: [HELP] Replace a line from .ini
 
Quote:

Originally Posted by DarthMan (Post 2588314)
Well, I did I was jsut pointing to your write_file, explaining that it's better to use the method with fputs.

Ofcourse new file natives are better than the oldones, they kept the oldones for compatibility, but doesnt mean you cant use them.

DarthMan 04-18-2018 10:59

Re: [HELP] Replace a line from .ini
 
Quote:

Originally Posted by Natsheh (Post 2588318)
Ofcourse new file natives are better than the oldones, they kept the oldones for compatibility, but doesnt mean you cant use them.

Yup, I know that :-)

HamletEagle 04-18-2018 12:58

Re: [HELP] Replace a line from .ini
 
Quote:

Originally Posted by DarthMan (Post 2588283)
Using array and the newer file system with fopen, fputs & fclose is the better way to go.

You don't need an array, it's totally pointless.

PHP Code:

stock RemoveLine(const FileName[], const OldLine[], const NewLine[])
{
    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))
                {
                    continue
                }
                
fprintf(InputFilePointer"%s^n"FileData)
            }
            
            
fclose(InputFilePointer)
            
fclose(FilePointer)

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


This is a function that I wrote in the past to remove a line from file. To replace instead of remove put a fprintf before continue with the new string.


All times are GMT -4. The time now is 04:34.

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