Raised This Month: $32 Target: $400
 8% 

Solved Manipulate string in file


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
iceeedr
Veteran Member
Join Date: Apr 2017
Location: Brazil
Old 12-15-2020 , 17:07   Manipulate string in file
Reply With Quote #1

Is there a way to pull a line up without leaving a space? when using "^r" there is an "invisible space on the line.

Example:

PHP Code:
0

if(iFile)
{
    new 
szData[120], szData1[120]

    while(
fgets(iFileszDatacharsmax(szData)))
    {
        
i++
        switch(
szData[0])
        {
            case 
EOS';''/': continue
            default:
            {
                
parse(szDataszData1charsmax(szData1))

                if(
equal(szData1example))
                {
                    
write_file(szConfig"^r"i-1)                        
                    break
                }
            }
        }
    }

    
fclose(iFile)

__________________


Quote:
Originally Posted by fysiks View Post
Please stop trying to help. You appear to just be posting random stuff. Wait until you actually understand more about AMX Mod X and how the game works.
https://iceeedr.com.br/

Last edited by iceeedr; 12-16-2020 at 10:58.
iceeedr is offline
Send a message via Skype™ to iceeedr
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 12-15-2020 , 21:35   Re: Manipulate string in file
Reply With Quote #2

Provide the sample output so I don't need to create a text file, compile your plugin, and test it myself.

Easiest may be just to set the last character to EOS.
__________________
Bugsy is offline
iceeedr
Veteran Member
Join Date: Apr 2017
Location: Brazil
Old 12-15-2020 , 21:43   Re: Manipulate string in file
Reply With Quote #3

The result looks like the example below:
PHP Code:
[Sector 1]
blablabla

[Sector2
So, in the plugin I detect the line that contains "blablabla" and use "^r"

So the text looks like this:
PHP Code:
[Sector 1]
"invisible space"

[Sector2
The line is not "empty" it is as if I gave a space and skipped the line.
__________________


Quote:
Originally Posted by fysiks View Post
Please stop trying to help. You appear to just be posting random stuff. Wait until you actually understand more about AMX Mod X and how the game works.
https://iceeedr.com.br/
iceeedr is offline
Send a message via Skype™ to iceeedr
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 12-15-2020 , 22:06   Re: Manipulate string in file
Reply With Quote #4

I am having no issue. Though, I am working on very little.. you have not said what exactly you are trying to do or what the source file contents look like. If you need help, provide the problem at hand, not your troubleshooting.

PHP Code:
public a()
{
    new 
iFile fopen"abc123.txt""rt" );
    
    new 
0
    
    
if(iFile)
    {
        new 
szData[120], szData1[120]
    
        while(
fgets(iFileszDatacharsmax(szData)))
        {
            
i++
            switch(
szData[0])
            {
                case 
EOS';''/': continue
                default:
                {
                    new 
iz parse(szDataszData1charsmax(szData1))
    
                    
server_print("[%s]" szData1)                        
                        
                
                }
            }
        }
    
        
fclose(iFile)
    }

Source file
Code:
Test line1
Test line2
Test line3
Test line4
Test line5
Test line6
Test line7
Test line8
T1119
T1110
T1111
T1112
Output
PHP Code:
[Test]
[
Test]
[
Test]
[
Test]
[
Test]
[
Test]
[
Test]
[
Test]
[
T1119]
[
T1110]
[
T1111]
[
T1112
__________________

Last edited by Bugsy; 12-15-2020 at 22:16.
Bugsy is offline
iceeedr
Veteran Member
Join Date: Apr 2017
Location: Brazil
Old 12-15-2020 , 22:26   Re: Manipulate string in file
Reply With Quote #5

Quote:
Originally Posted by Bugsy View Post
I am having no issue. Though, I am working on very little.. you have not said what exactly you are trying to do or what the source file contents look like. If you need help, provide the problem at hand, not your troubleshooting.

PHP Code:
public a()
{
    new 
iFile fopen"abc123.txt""rt" );
    
    new 
0
    
    
if(iFile)
    {
        new 
szData[120], szData1[120]
    
        while(
fgets(iFileszDatacharsmax(szData)))
        {
            
i++
            switch(
szData[0])
            {
                case 
EOS';''/': continue
                default:
                {
                    new 
iz parse(szDataszData1charsmax(szData1))
    
                    
server_print("[%s]" szData1)                        
                        
                
                }
            }
        }
    
        
fclose(iFile)
    }

Source file
Code:
Test line1
Test line2
Test line3
Test line4
Test line5
Test line6
Test line7
Test line8
T1119
T1110
T1111
T1112
Output
PHP Code:
[Test]
[
Test]
[
Test]
[
Test]
[
Test]
[
Test]
[
Test]
[
Test]
[
T1119]
[
T1110]
[
T1111]
[
T1112
I'll be as clear as possible, because I don't think you understand.

Let's use the function below as a test to remove an admin for example, after finding the line with steamid I remove it ok?

PHP Code:
public RemoveAdminHandler(id)
{
    
get_user_authid(g_AdminTarget[id], playerinfocharsmax(playerinfo));

    new 
szConfig[MAX_FMT_LENGTH]
    
get_configsdir(szConfigcharsmax(szConfig))
    
add(szConfigcharsmax(szConfig), "/users.ini")

    new 
iFile fopen(szConfig"rt")

    
0

    
if(iFile)
    {
        new 
szData[120], szData1[120]

        while(
fgets(iFileszDatacharsmax(szData)))
        {
            
i++
            switch(
szData[0])
            {
                case 
EOS';''/''[': continue
    
                default:
                {
                    
parse(szDataszData1charsmax(szData1))

                    if(
equal(szData1playerinfo))
                    {
                        
write_file(szConfig"^r"i-1)
                        
server_cmd("amx_reloadadmins")
                        break
                    }
                }
            }
        }

        
fclose(iFile)
    }

    return 
PLUGIN_HANDLED

In the attachment I put user.ini (which is the test before deletion) and users - after.ini (which is after removing the line), notice that in the 1st line of the "after" file there is an "invisible" space.
Attached Files
File Type: ini users - after.ini (1 Bytes, 41 views)
File Type: ini users.ini (47 Bytes, 33 views)
__________________


Quote:
Originally Posted by fysiks View Post
Please stop trying to help. You appear to just be posting random stuff. Wait until you actually understand more about AMX Mod X and how the game works.
https://iceeedr.com.br/
iceeedr is offline
Send a message via Skype™ to iceeedr
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 12-15-2020 , 23:09   Re: Manipulate string in file
Reply With Quote #6

I would change your approach. Create an entirely new temp file with lines that should be there and when done, delete the original and do a rename on the temp. Backup your users.ini beforehand.
PHP Code:
public RemoveAdminHandlerid 
{
    
get_user_authid(g_AdminTarget[id], playerinfocharsmax(playerinfo));

    new 
szConfigTemp[MAX_FMT_LENGTH];
    new 
szConfig[MAX_FMT_LENGTH];
    
get_configsdir(szConfigcharsmax(szConfig))
    
add(szConfigcharsmax(szConfig), "/users.ini")
    
add(szConfigTempcharsmax(szConfigTemp), "/usersTemp.ini");
    
    new 
iFile fopen(szConfig"rt")
    
    if(
iFile)
    {
        new 
szData[120], szData1[120]

        while(
fgets(iFileszDatacharsmax(szData)))
        {
            switch(
szData[0])
            {
                case 
EOS';''/''['write_file(szConfigTempszData)
                default:
                {
                    
parse(szDataszData1charsmax(szData1))
                    
                    if(!
equal(szData1playerinfo))
                    {
                        
write_file(szConfigTempszData1)
                    }
                }
            }
        }

        
fclose(iFile)
    }
    

    
delete_fileszConfig );
    
rename_fileszConfigTemp szConfig );

    
server_cmd("amx_reloadadmins")

    return 
PLUGIN_HANDLED

__________________

Last edited by Bugsy; 12-15-2020 at 23:13.
Bugsy is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 12-15-2020 , 23:19   Re: Manipulate string in file
Reply With Quote #7

Calling it an "invisible" space is redundant because it's just the regular space character (0x20), what you get when you press your space bar.

When I use:

PHP Code:
write_file("test.txt""^r"2
on this file:
Code:
hello
world
good
bye
I get:
Code:
hello
world

bye

Also, the fact that you're trying to edit a file with write_file() while that same file is open with fopen() is a problem in general (I'm not sure if AMX Mod X has any safe guards in place to prevent bad behavior when you do this).
__________________
fysiks is offline
iceeedr
Veteran Member
Join Date: Apr 2017
Location: Brazil
Old 12-16-2020 , 10:58   Re: Manipulate string in file
Reply With Quote #8

Our worst enemy is fatigue, taking into account all the help and even what was punctuated by the fysiks, we arrived at the code below which is apparently functional, thanks.

PHP Code:
new szConfigTemp[MAX_FMT_LENGTH];
new 
szConfig[MAX_FMT_LENGTH];
get_configsdir(szConfigcharsmax(szConfig))
add(szConfigcharsmax(szConfig), "/users.ini")
add(szConfigTempcharsmax(szConfigTemp), "/usersTemp.ini");

new 
iFile fopen(szConfig"r")
new 
iFile2 fopen(szConfigTemp"w")
if(
iFile && iFile2)
{
    new 
szData[120], szData1[120]

    while(
fgets(iFileszDatacharsmax(szData)))
    {
            switch(
szData[0])
            {
                case 
EOS';''/''['fputs(iFile2szData)
                default:
                {
                    
parse(szDataszData1charsmax(szData1))

                    if(!
equal(szData1playerinfo))
                    {
                        
fputs(iFile2szData)
                    }
                }
            }
    }

    
fclose(iFile)
    
fclose(iFile2)
}

delete_fileszConfig );
rename_fileszConfigTemp szConfig ); 
__________________


Quote:
Originally Posted by fysiks View Post
Please stop trying to help. You appear to just be posting random stuff. Wait until you actually understand more about AMX Mod X and how the game works.
https://iceeedr.com.br/
iceeedr is offline
Send a message via Skype™ to iceeedr
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 12-16-2020 , 17:05   Re: Manipulate string in file
Reply With Quote #9

Cool, pretty much the proper way to code what I provided.
__________________
Bugsy is offline
iceeedr
Veteran Member
Join Date: Apr 2017
Location: Brazil
Old 12-16-2020 , 17:08   Re: Manipulate string in file
Reply With Quote #10

Quote:
Originally Posted by Bugsy View Post
Cool, pretty much the proper way to code what I provided.
I am sorry because even though I am able to write and read English fluently, sometimes I do not understand when the phrase is sarcasm or real, so I will understand it as a compliment. If not, I apologize because the credit is yours and the fysiks for the help, thank you.
__________________


Quote:
Originally Posted by fysiks View Post
Please stop trying to help. You appear to just be posting random stuff. Wait until you actually understand more about AMX Mod X and how the game works.
https://iceeedr.com.br/
iceeedr is offline
Send a message via Skype™ to iceeedr
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 23:54.


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