Raised This Month: $ Target: $400
 0% 

Help about Reading from file strings.


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Craxor
Veteran Member
Join Date: Jan 2016
Location: Romania
Old 01-27-2016 , 03:44   Help about Reading from file strings.
Reply With Quote #1

The problem: is reading only the first line, sorry, i'm noob at reading files, thank's for help:

Code:
#include <amxmodx>
#include <amxmisc>
#include <fakemeta>

new const PLUGIN[]	= "Block Pick UP by MODEL",
	 VERSION[]	= "0.1",
	  AUTHOR[]	= "Craxor";

new filename[256];
new g_Model[60];
new buffer[60];

public plugin_init( )
{
	register_plugin
	(
		.plugin_name = PLUGIN, 
		.version     = VERSION, 
		.author      = AUTHOR
	)

	register_forward( FM_Touch, "WeaponBk" );
}

public plugin_cfg( )
{
	get_configsdir(filename,255)
	format(filename,255,"%s/blockpuw.ini",filename)

 
	new filepointer = fopen(filename,"r")

	if(filepointer)
	{
		new readdata[128];

		while(fgets(filepointer,readdata,charsmax( readdata)))
		{
			parse(readdata,g_Model, charsmax( g_Model ))
			formatex( buffer, charsmax( buffer ), "models/w_%s.mdl", g_Model);
			break;
		}
     
		fclose(filepointer)
	}
}
 
public WeaponBk( Entity, Id )
{
	if (!pev_valid(Entity) || !pev_valid(Id) || !is_user_alive(Id))
		return FMRES_IGNORED;

        new Model[32];
        pev(Entity, pev_model, Model, charsmax( Model ) );

	if( equali ( Model, buffer ) )
		return FMRES_SUPERCEDE;	

	return FMRES_IGNORED;
}
__________________
Project: Among Us

Last edited by Craxor; 01-27-2016 at 05:06.
Craxor is offline
Send a message via ICQ to Craxor
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 01-27-2016 , 19:51   Re: Help about Reading from file strings.
Reply With Quote #2

Your while structure should be:

PHP Code:
while( !feof(filepointer) )
{
    
fgets(...) 
This checks the current line to see if it is the "end of file" (EOF). Therefore, this code will read all lines until the end of the file.
__________________

Last edited by fysiks; 01-27-2016 at 21:10.
fysiks is offline
Craxor
Veteran Member
Join Date: Jan 2016
Location: Romania
Old 01-28-2016 , 01:18   Re: Help about Reading from file strings.
Reply With Quote #3

Is acting the same, reading only first line:

Code:
#include <amxmodx>
#include <amxmisc>
#include <fakemeta>

new const PLUGIN[]	= "Blocking Pick UP Weapon",
	 VERSION[]	= "0.1",
	  AUTHOR[]	= "Craxor";

new filename[256];
new buffer[60];

public plugin_init( )
{
	register_plugin
	(
		.plugin_name = PLUGIN, 
		.version     = VERSION, 
		.author      = AUTHOR
	)

	register_forward( FM_Touch, "WeaponBk" );
}

public plugin_cfg( )
{
	get_configsdir( filename, charsmax( filename ) )
	format( filename, charsmax( filename ), "%s/blockpuw.ini", filename )

 
	new filepointer = fopen( filename, "r" )

	if( filepointer )
	{
		new readdata[128], parsemodel[32];

		while( !feof( filepointer) )
		{
			fgets( filepointer,readdata,charsmax( readdata) )
			parse( readdata, parsemodel, charsmax( parsemodel ) )
			formatex( buffer, charsmax( buffer ), "models/w_%s.mdl", parsemodel );
			break;
		}
     
		fclose( filepointer )
	}
}
 
public WeaponBk( Entity, Id )
{
	if ( !pev_valid( Entity ) || !pev_valid( Id ) || !is_user_alive( Id ) )
		return FMRES_IGNORED;

        new Model[32];
        pev(Entity, pev_model, Model, charsmax( Model ) );

	if( equali ( Model, buffer ) )
		return FMRES_SUPERCEDE;	

	return FMRES_IGNORED;
}
__________________
Project: Among Us

Last edited by Craxor; 01-28-2016 at 01:49.
Craxor is offline
Send a message via ICQ to Craxor
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 01-28-2016 , 05:59   Re: Help about Reading from file strings.
Reply With Quote #4

Why are you inserting "break" in the while loop?
__________________
Arkshine is offline
Craxor
Veteran Member
Join Date: Jan 2016
Location: Romania
Old 01-28-2016 , 09:19   Re: Help about Reading from file strings.
Reply With Quote #5

Quote:
Originally Posted by Arkshine View Post
Why are you inserting "break" in the while loop?
@Edit@ : I removed "break" and is stil block only first weapon from list.

That's the example i see in this topic: https://forums.alliedmods.net/showthread.php?t=46218

COde:

PHP Code:
#include <amxmodx>
#include <amxmisc>

#define PLUGIN "Filehandle"
#define VERSION "1.0"
#define AUTHOR "Administrator"

new filename[256]

public 
plugin_init() {
    
register_plugin(PLUGINVERSIONAUTHOR)
    
    
get_configsdir(filename,255)
    
format(filename,255,"%s/filehandle.txt",filename)
    
register_concmd("amx_filetestwrite","filewritetest")
    
register_concmd("amx_filetestread","filereadtest")
}

public 
filewritetest(id){
    new 
writedata[128]
    new 
steamid[32],name[32],anyvalue,Float:anyfloatvalue
    
    get_user_authid
(id,steamid,31)
    
get_user_name(id,name,31)
    
anyvalue 10
    anyfloatvalue 
5.5
    
    
/*fopen - Returns a file handle for advanced file functions. 
    Mode uses the standard C library of mode types.
    The first character can be:
    "a" - append
    "r" - read
    "w" - write
    
    The second character can be:
    "t" - text
    "b" - binary
    
    You can also append a "+" to specify both read and write.
    Open a file in append+read+write mode
    On mode "a+"/"w+" it create a file if there no exist
    Mode "w+" overwrite a file if one exist*/
    
new filepointer fopen(filename,"a+")

    
/*check if file is open,on an error filepointer is 0*/
    
if(filepointer)
    {
        
/*It give 2 ways to write a string inside a file*/
        
        /*First way*/
        
formatex(writedata,127,"%s %s %d %f^n",steamid,name,anyvalue,anyfloatvalue)
        
//The new file commands need to create a newline manual with "^n"
        
        /*write the string to the file*/
        /*another commands are:
        fputc - Writes a char (1 byte) to a file handle.
        fputf - Writes a float (4 bytes, 8 on AMD64) to a file handle. 
        fputi - Writes an integer (4 bytes) to a file handle. 
        fputl - Writes a long (4 bytes) to a file handle. */
        
fputs(filepointer,writedata)
        
        
/*Second way*/
        /*fprintf - Writes a line to a file */
        
fprintf(filepointer,"%s %s %d %f^n",steamid,name,anyvalue,anyfloatvalue)
        
        
/*close the file,this is optional,but better is it to close the file*/
        
fclose(filepointer)
    }
}

public 
filereadtest(id){
    
/*open file in read-mode*/
    
new filepointer fopen(filename,"r")
    
/*check if file is open,on an error filepointer is 0*/
    
if(filepointer)
    {
        new 
readdata[128],steamid[32],anyvalue,Float:anyfloatvalue
        
new parsedsteamid[32],parsedname[32],parsedanyvalue[8],parsedanyfloatvalue[8]
    
        
/*Read the file until it is at end of file*/
        /*fgets - Reads a line from a text file -- includes newline!*/
        
while(fgets(filepointer,readdata,127))
        {   
            
parse(readdata,parsedsteamid,31,parsedname,31,parsedanyvalue,7,parsedanyfloatvalue,7)
        
            
get_user_authid(id,steamid,31)
            if(
equal(steamid,parsedsteamid))
            {
                
anyvalue str_to_num(parsedanyvalue)
                
anyfloatvalue str_to_float(parsedanyfloatvalue)
                
client_print(id,print_chat,"Your saved Steamid:%s Name:%s Value:%d FloatValue:%f",parsedsteamid,parsedname,anyvalue,anyfloatvalue)
                break
                
//...
            
}
        }
        
fclose(filepointer)
    }

__________________
Project: Among Us

Last edited by Craxor; 01-28-2016 at 09:24.
Craxor is offline
Send a message via ICQ to Craxor
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 01-28-2016 , 19:52   Re: Help about Reading from file strings.
Reply With Quote #6

Remove the "break". That causes the loop to quit execution (i.e. to break out of the loop).

Also, the return value of fgets() is undocumented so it's best to not use it as the condition at all. Using !feof() is more appropriate.
__________________

Last edited by fysiks; 01-28-2016 at 19:54.
fysiks is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 01-28-2016 , 20:16   Re: Help about Reading from file strings.
Reply With Quote #7

fgets() returns the number of characters read in the line, when you reach the end of the file it will output 0. feof() is still the proper choice, even though using fgets() as the condition technically could still work.

Code:
// native fgets(file, buffer[], maxlength);
static cell AMX_NATIVE_CALL amx_fgets(AMX *amx, cell *params)
{
	FileObject* fp = reinterpret_cast<FileObject*>(params[1]);

	if (!fp)
	{
		return 0;
	}

	static char buffer[4096];
	buffer[0] = '\0';

	fp->ReadLine(buffer, sizeof(buffer) - 1);

	return set_amxstring_utf8(amx, params[2], buffer, strlen(buffer), params[3]);
}
Using a file named "test.txt" with the below contents:
Code:
AAAAA
BBBBB
CCCCC
it gives the below output (notice it will read the new line character)
Code:
6 chars = AAAAA

6 chars = BBBBB

5 chars = CCCCC
EOF reached
PHP Code:
new iFile fopen"test.txt" "rt" );
new 
szTest10 ] , iChars;
    
while ( ( 
iChars fgetsiFile szTest charsmaxszTest ) ) ) )
{
    
server_print"%d chars = %s" iChars szTest);
}
    
fcloseiFile );
    
server_print"EOF reached" ); 
__________________

Last edited by Bugsy; 01-28-2016 at 20:21.
Bugsy is offline
addons_zz
Veteran Member
Join Date: Aug 2015
Location: Dreams, zz
Old 01-29-2016 , 15:37   Re: Help about Reading from file strings.
Reply With Quote #8

You forward 'fwd_Touch' is called a lot, just to not pick up weapons. Could you just hook WeaponPickUp event? See for help:
  1. Block weapon pick-up without c4
  2. [SNIPPET] Properly hook and block weapons pickup
Code:
register_forward( FM_Touch, "WeaponBk" );
Quote:
Originally Posted by Craxor View Post
Is acting the same, reading only first line:
Try use contain instead of equali. Or save each one of them to a trie.
Code:
if( equali ( Model, buffer ) )
What you are doing now is put everything in sequence on buffer[] and see if the weapon you touched is equal to the entire buffer[] array. But this will never be true. What could happen on most best case, is you compare the weapon you touched with the first weapon name on buffer[] array. So, because of that, I said to you use contain(), instead of equali().
__________________
Plugin: Sublime Text - ITE , Galileo
Multi-Mod: Manager / Plugin / Server

Support me on Patreon, Ko-fi, Liberapay or Open Collective

Last edited by addons_zz; 01-29-2016 at 19:19. Reason: spelling fix
addons_zz is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 01-29-2016 , 16:21   Re: Help about Reading from file strings.
Reply With Quote #9

addons_zz, he wants to block a weapon retrieval, such event is not going to help. Same for "contain", it's not going to help. If he wants to block models from a list, then trie yes should be appropriate.
__________________
Arkshine is offline
Craxor
Veteran Member
Join Date: Jan 2016
Location: Romania
Old 02-01-2016 , 02:11   Re: Help about Reading from file strings.
Reply With Quote #10

Working fine with trie.

But appear other problem, when i try to change the map all key's from Trie is removed, how i can save/load keys from Trie?

(TrieSnapshotCreate is working only for 183)

Code:
#include <amxmodx>
#include <amxmisc>
#include <fakemeta>

new const PLUGIN[]	= "Pick Up Manager",
	 VERSION[]	= "0.2",
	  AUTHOR[]	= "Craxor";

new Trie:g_tBlock;
new giTypeCvar;

public plugin_init( )
{
	register_plugin
	(
		.plugin_name = PLUGIN, 
		.version     = VERSION, 
		.author      = AUTHOR
	)

	g_tBlock = TrieCreate( );

	register_forward( FM_Touch, "WeaponTouchFwd" );

	register_concmd( "amx_pum_addkey" ,"addkey", ADMIN_BAN, " < Weapon key-name to block > ");
	register_concmd( "amx_pum_remkey" ,"remkey", ADMIN_BAN, " < Weapon key-name to block > ");

	giTypeCvar = register_cvar("pum_type", "1" );
}

public addkey( id, level, cid )
{
	if( !cmd_access( id, level, cid, 2 ) )
		return PLUGIN_HANDLED;

	new Arg1[16];
	read_argv( 1, Arg1, charsmax( Arg1 ) );

	new adder[64];
	formatex( adder, charsmax( adder ), "models/w_%s.mdl", Arg1 );

	TrieSetCell( g_tBlock, adder, 1 );
	return PLUGIN_HANDLED;
}

public remkey( id, level, cid )
{
	if( !cmd_access( id, level, cid, 2 ) )
		return PLUGIN_HANDLED;

	new Arg1[16];
	read_argv( 1, Arg1, charsmax( Arg1 ) );

	new adder[64];
	formatex( adder, charsmax( adder ), "models/w_%s.mdl", Arg1 );

	TrieDeleteKey( g_tBlock, adder );
	return PLUGIN_HANDLED;
}
 
public WeaponTouchFwd( Entity, Id )
{
	if ( !pev_valid( Entity ) || !pev_valid( Id ) || !is_user_alive( Id ) )
		return FMRES_IGNORED;

        new Model[32];
        pev(Entity, pev_model, Model, charsmax( Model ) );

	new iCvarValue = get_pcvar_num( giTypeCvar );

	if( TrieKeyExists( g_tBlock, Model ) )
	{
		switch( iCvarValue )
		{
			case 1: engfunc( EngFunc_RemoveEntity, Entity );
			case 2: return FMRES_SUPERCEDE;
			default: return FMRES_IGNORED;
		}
	}
	return FMRES_IGNORED;
}
__________________
Project: Among Us

Last edited by Craxor; 02-01-2016 at 08:49.
Craxor is offline
Send a message via ICQ to Craxor
Reply


Thread Tools
Display Modes

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 09:21.


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