Raised This Month: $ Target: $400
 0% 

Solved fseek cannot function as expected


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Scrooge2029
Junior Member
Join Date: Dec 2020
Old 12-27-2020 , 05:52   fseek cannot function as expected
Reply With Quote #1

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.

If anyone could answer my question, I'd be very appreciate for it, Thx!
Attached Files
File Type: txt default_plugins.txt (97 Bytes, 25 views)

Last edited by Scrooge2029; 12-28-2020 at 03:56. Reason: Solved
Scrooge2029 is offline
Scrooge2029
Junior Member
Join Date: Dec 2020
Old 12-28-2020 , 03:56   Re: fseek cannot function as expected
Reply With Quote #2

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)
Scrooge2029 is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 12-28-2020 , 11:23   Re: fseek cannot function as expected
Reply With Quote #3

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)

__________________

Last edited by Bugsy; 12-28-2020 at 11:24.
Bugsy is offline
Scrooge2029
Junior Member
Join Date: Dec 2020
Old 12-28-2020 , 21:47   Re: fseek cannot function as expected
Reply With Quote #4

Quote:
Originally Posted by Bugsy View Post
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!
Scrooge2029 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 10:05.


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