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

Solved [HELP] Replace a line from .ini


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
CrAzY MaN
Senior Member
Join Date: Mar 2017
Location: India
Old 04-18-2018 , 03:39   [HELP] Replace a line from .ini
Reply With Quote #1

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.
__________________

Last edited by CrAzY MaN; 05-20-2018 at 08:19.
CrAzY MaN is offline
DarthMan
Veteran Member
Join Date: Aug 2011
Old 04-18-2018 , 04:03   Re: [HELP] Replace a line from .ini
Reply With Quote #2

Quote:
Originally Posted by CrAzY MaN View Post
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.
DarthMan is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 04-18-2018 , 04:06   Re: [HELP] Replace a line from .ini
Reply With Quote #3

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.
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !


Last edited by Natsheh; 04-18-2018 at 04:08.
Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
DarthMan
Veteran Member
Join Date: Aug 2011
Old 04-18-2018 , 04:16   Re: [HELP] Replace a line from .ini
Reply With Quote #4

Quote:
Originally Posted by Natsheh View Post
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.
DarthMan is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 04-18-2018 , 05:56   Re: [HELP] Replace a line from .ini
Reply With Quote #5

Quote:
Originally Posted by DarthMan View Post
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.
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !

Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 04-18-2018 , 07:14   Re: [HELP] Replace a line from .ini
Reply With Quote #6

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
__________________
Bugsy is offline
DarthMan
Veteran Member
Join Date: Aug 2011
Old 04-18-2018 , 08:48   Re: [HELP] Replace a line from .ini
Reply With Quote #7

Quote:
Originally Posted by Natsheh View Post
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.
DarthMan is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 04-18-2018 , 09:12   Re: [HELP] Replace a line from .ini
Reply With Quote #8

Quote:
Originally Posted by DarthMan View Post
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.
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !

Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
DarthMan
Veteran Member
Join Date: Aug 2011
Old 04-18-2018 , 10:59   Re: [HELP] Replace a line from .ini
Reply With Quote #9

Quote:
Originally Posted by Natsheh View Post
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 :-)
DarthMan is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 04-18-2018 , 12:58   Re: [HELP] Replace a line from .ini
Reply With Quote #10

Quote:
Originally Posted by DarthMan View Post
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.
__________________
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 12:45.


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