AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved Add character in existing line (https://forums.alliedmods.net/showthread.php?t=309973)

edon1337 08-14-2018 06:36

Add character in existing line
 
I'm trying to add a character in front of an existing string, and then replace the old line inside the file with the new edited one.

For example:

Current:
PHP Code:

"Rabbit" 

Edited:
PHP Code:

;"Rabbit" 

Any idea?

HamletEagle 08-14-2018 06:45

Re: Add character in existing line
 
It's not very hard:
1.Create a temporary file.
2.Open the original file and read line by line.
3.Write all lines from the original file to the temporary file exactly how they are, except the desired line, which you edit and then write.
4.Delete the original file.
5.Rename the temporary file to the original name.

edon1337 08-14-2018 06:49

Re: Add character in existing line
 
Quote:

Originally Posted by HamletEagle (Post 2610141)
It's not very hard:
1.Create a temporary file.
2.Open the original file and read line by line.
3.Write all lines from the original file to the temporary file exactly how they are, except the desired line, which you edit and then write.
4.Delete the original file.
5.Rename the temporary file to the original name.

I'm aware of that already, but isn't there another way?

klippy 08-14-2018 06:53

Re: Add character in existing line
 
Quote:

Originally Posted by edon1337 (Post 2610142)
I'm aware of that already, but isn't there another way?

No, that's how files work, they are streams, it's not AMXX's thing.

edon1337 08-14-2018 06:54

Re: Add character in existing line
 
Quote:

Originally Posted by KliPPy (Post 2610143)
No, that's how files work, they are streams, it's not AMXX's thing.

Delete the line and write a new one at it's place?

klippy 08-14-2018 06:58

Re: Add character in existing line
 
Quote:

Originally Posted by edon1337 (Post 2610144)
Delete the line and write a new one at it's place?

It doesn't work that way. It will only work provided the the new and old lines have the same length. As I said, files are streams, they can only be appended to or overwritten, you can't insert in the middle or delete content.

edon1337 08-14-2018 07:03

Re: Add character in existing line
 
Quote:

Originally Posted by KliPPy (Post 2610145)
It doesn't work that way. It will only work provided the the new and old lines have the same length. As I said, files are streams, they can only be appended to or overwritten, you can't insert in the middle or delete content.

Ah, I see. I'll use this then, thanks https://forums.alliedmods.net/showpo...56&postcount=5

edon1337 08-14-2018 07:06

Re: Add character in existing line
 
There's a problem though, even with that stock, how am I supposed to add a character at the start of the text? If I replace " with ;" it will be like this
PHP Code:

;"Donii;" 

Meanwhile it should be like this
PHP Code:

;"Donii" 


HamletEagle 08-14-2018 07:10

Re: Add character in existing line
 
Quote:

Originally Posted by edon1337 (Post 2610147)
There's a problem though, even with that stock, how am I supposed to add a character at the start of the text? If I replace " with ;" it will be like this
PHP Code:

;"Donii;" 

Meanwhile it should be like this
PHP Code:

;"Donii" 


That's basic string manipulation, nothing to do with files.
1.Shift all cell's content one position to the right and then overwrite the first one.
2.Use formatex in another string, like formatex(..., ..., ";%s", Line).
3.Use strcat.

edon1337 08-14-2018 07:25

Re: Add character in existing line
 
Quote:

Originally Posted by HamletEagle (Post 2610148)
That's basic string manipulation, nothing to do with files.
1.Shift all cell's content one position to the right and then overwrite the first one.
2.Use formatex in another string, like formatex(..., ..., ";%s", Line).
3.Use strcat.

I totally forgot about formatex :oops:

Is NewLine[] purposely not used or you just forgot?
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




All times are GMT -4. The time now is 14:18.

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