AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved fseek cannot function as expected (https://forums.alliedmods.net/showthread.php?t=329504)

Scrooge2029 12-27-2020 05:52

fseek cannot function as expected
 
1 Attachment(s)
Hi everyone, I am a freshman start to write amxx scripts recently, I want to start with a file logger as my practice:), when I try to append additional text to the second to the last line, weird thing (or it's just my misuse of file operation functions) happend.

1.open file with "at", I cannot set cursor for the file, and the text is always append to the last
2.open file with "wt", I can set cursor but the position is no counted by lines but bytes or bits? I set cursor using fseek to the 30th line (if the second param of fseek represents the target lineNo), but ended up with a new text file with 30 ? symbol (could not be recognize by utf-8 decoder) appended by my new text

amxx:
Code:

AMX Mod X 1.8.2 (http://www.amxmodx.org)
Authors:
        David "BAILOPAN" Anderson, Pavol "PM OnoTo" Marko
        Felix "SniperBeamer" Geyer, Jonny "Got His Gun" Bergstrom
        Lukasz "SidLuke" Wlasinski, Christian "Basic-Master" Hammacher
        Borja "faluco" Ferrer, Scott "DS" Ehlert
Compiled: Feb 14 2013, 00:56:37
Build ID: 1.8.2 61:2ae84f7c0a97
Core mode: JIT+ASM32

my code:
Code:

public reload_as()
{
        new text[128]
        text=""
        strcat(text,"^"plugin^"{^n^"name^" ^"",128)
        strcat(text,Fname,128)
        strcat(text,"^"^n^"script^" ^"",128)
        strcat(text,Fname,128)
        strcat(text,"^"^n}^n^n}",128)
        new pList=fopen("default_plugins.txt","wt")
        fseek(pList, 0, SEEK_END)
        new LineNo=ftell(pList)
        new LN[32]
        num_to_str(LineNo,LN,32)
        server_cmd(LN)
        fseek(pList, 30, SEEK_CUR)
        LineNo=ftell(pList)
        num_to_str(LineNo,LN,32)
        server_cmd(LN)
        fputs(pList, text)
        fclose(pList)
        //server_cmd("as_reloadplugins")
}

The Fname is a global string with length no more than 32

before execution, the target file (UTF-8,CRLF):
Code:

//========================================
//
//        This file lists all plugins that are loaded by the game
//        Plugins use Angelscript
//
//========================================
"plugins"
{
        "plugin"
        {
                "name" "PlayerManagement"
                "script" "admin/PlayerManagement"
                "concommandns" "plrmgr"
        }
        "plugin"
        {
                "name" "RCBot"
                "script" "BotManager/BotManager"
                "concommandns" "rcbot"
        }
        "plugin"
        {
                "name" "HookLogger"
                "script" "HookLogger"
        }
        "plugin"
        {
                "name" "UREnhanced"
                "script" "UREnhanced"
        }

}

After execution:
Code:

??????????????????????????????"plugin"{
"name" "UREnhanced.as"
"script" "UREnhanced.as"
}

}

? cannot be copied and pasted, it's a ? but with a ♦ on its back, UREnhanced.as is the Fname I mentioned above, the attached file is this text file.

It's my first time posting a thread, if there is any violation against rules, I sincerely apologize.:oops::cry:

If anyone could answer my question, I'd be very appreciate for it, Thx!:)

Scrooge2029 12-28-2020 03:56

Re: fseek cannot function as expected
 
This is how I solved this problem:

Code:

        new text[128]
        text=""
        strcat(text,"^"plugin^"{^n^"name^" ^"",128)
        strcat(text,Fname,128)
        strcat(text,"^"^n^"script^" ^"",128)
        strcat(text,Fname,128)
        strcat(text,"^"^n}^n^n}",128)
        new pList=fopen("default_plugins.txt","r+")
        fseek(pList,-2,SEEK_END)
        fprintf(pList, "%s", text)
        fclose(pList)


Bugsy 12-28-2020 11:23

Re: fseek cannot function as expected
 
You can avoid using fseek() by adding 2 new-lines (^n^n) at the end of your text. Since you currently end your file write with "}", the next append is going to begin immediately after that character, it does not know that you first want to insert 2 new-lines before writing the new text.

Code:

strcat(text,"^"^n}^n^n}",128)
//to
strcat(text,"^"^n}^n^n}^n^n", charsmax( text ) )

Also, if your string is already prepared/formatted for writing (which it is since you used strcat()), you do not need to use fprintf(), you can use fputs() instead.

fprint() would be appropriate if you wanted to do this.
PHP Code:

new text128 ]
new const 
Fname[] = "test";
    
new 
pList fopen"default_plugins.txt" "a" )
    
if ( 
pList )
{
    
fprintf(pList"^"plugin^"{^n^"name^" ^"%s^"^n^"scRipt^" ^"%s^"^n}^n^n}^n^n" Fname Fname )
    
fclose(pList)



Scrooge2029 12-28-2020 21:47

Re: fseek cannot function as expected
 
Quote:

Originally Posted by Bugsy (Post 2730465)
You can avoid using fseek() by adding 2 new-lines (^n^n) at the end of your text. Since you currently end your file write with "}", the next append is going to begin immediately after that character, it does not know that you first want to insert 2 new-lines before writing the new text.

Code:

strcat(text,"^"^n}^n^n}",128)
//to
strcat(text,"^"^n}^n^n}^n^n", charsmax( text ) )

Also, if your string is already prepared/formatted for writing (which it is since you used strcat()), you do not need to use fprintf(), you can use fputs() instead.

fprint() would be appropriate if you wanted to do this.
PHP Code:

new text128 ]
new const 
Fname[] = "test";
    
new 
pList fopen"default_plugins.txt" "a" )
    
if ( 
pList )
{
    
fprintf(pList"^"plugin^"{^n^"name^" ^"%s^"^n^"scRipt^" ^"%s^"^n}^n^n}^n^n" Fname Fname )
    
fclose(pList)



Thanks for your guidance!:)


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

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