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

Solved how read more then one line fgets( )


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
nacknic
Senior Member
Join Date: Mar 2019
Old 04-01-2019 , 01:22   how read more then one line fgets( )
Reply With Quote #1

how read more then one line fgets( ) ?
Code:
...
while (feof(file) == 0)	
{
      fgets(file, argFile, charsmax(argFile));
}
its repeat the first line.

i know there function read_file( ), the index line is build-in.
but its take many resources, and slower:
Code:
native read_file(const file[], line, text[], len, &txtlen = 0);

Last edited by nacknic; 04-02-2019 at 02:42.
nacknic is offline
thEsp
BANNED
Join Date: Aug 2017
Old 04-01-2019 , 05:33   Re: how read more then one line fgets( )
Reply With Quote #2

Quote:
Originally Posted by nacknic View Post
how read more then one line fgets( ) ?
Code:
...
while (feof(file) == 0)	
{
      fgets(file, argFile, charsmax(argFile));
}
PHP Code:
while (!feof(file)) // Repeats until the file end is reached    
{
      
fgets(fileargFilecharsmax(argFile));

EDIT: Remember to close the file with fclose(), and take a look at this tutorial.

Last edited by thEsp; 04-01-2019 at 05:47.
thEsp is offline
nacknic
Senior Member
Join Date: Mar 2019
Old 04-01-2019 , 07:19   Re: how read more then one line fgets( )
Reply With Quote #3

Quote:
Originally Posted by thEsp View Post
PHP Code:
while (!feof(file)) // Repeats until the file end is reached    
{
      
fgets(fileargFilecharsmax(argFile));

EDIT: Remember to close the file with fclose(), and take a look at this tutorial.
its not my all code... i know about fclose( ). about your example, its equal to what i do, in amxmodx api said:

Code:
 Return: 1 if end of file has been reached, 0 otherwise.
so: while (!feof(file)) equal to while(feof(file) == 0) equal to while(feof(file) != 1)
the problem is fgets( ),its read line but i dont know how move it to the next line

Last edited by nacknic; 04-01-2019 at 07:21.
nacknic is offline
CrazY.
Veteran Member
Join Date: May 2015
Location: SP, Brazil
Old 04-01-2019 , 07:24   Re: how read more then one line fgets( )
Reply With Quote #4

fgets() already "move toward" lines, what means each repeat it's a new line.
__________________








CrazY. is offline
nacknic
Senior Member
Join Date: Mar 2019
Old 04-01-2019 , 07:35   Re: how read more then one line fgets( )
Reply With Quote #5

Quote:
Originally Posted by CrazY. View Post
fgets() already "move toward" lines, what means each repeat it's a new line.
where are you quoting from? if its true maybe problem with my code

EDIT: fgets( ) really go alone to the next line, its probably my code...

Last edited by nacknic; 04-01-2019 at 07:49.
nacknic is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 04-01-2019 , 07:47   Re: how read more then one line fgets( )
Reply With Quote #6

Quote:
Originally Posted by nacknic View Post
where are you quoting from? if its true maybe problem with my code
From nowhere, that's how fgets work. Show all the code that is about reading the file(open/read/close etc).
__________________
HamletEagle is offline
nacknic
Senior Member
Join Date: Mar 2019
Old 04-01-2019 , 08:37   Re: how read more then one line fgets( )
Reply With Quote #7

Quote:
Originally Posted by HamletEagle View Post
From nowhere, that's how fgets work. Show all the code that is about reading the file(open/read/close etc).
what the code supposed to do: when client_infochanged( ) called, its check from the file if the steamid
found its check if the nick its the same (file and the server) and if not , force to use the nick that wirte in the file:
Code:
/* Plugin generated by AMXX-Studio */
 
#include <amxmodx>
#include <amxmisc>
#include <fakemeta>
 
#define PLUGIN "New Plug-In"
#define VERSION "1.0"
#define AUTHOR "nacknic"
 

public plugin_init() register_plugin(PLUGIN, VERSION, AUTHOR);  
 
public client_infochanged(id) 
{
	new clientNick[51], clientId[22], storageId[22], storageNick[51],  i = 1;
	get_user_info(id, "name", clientNick, charsmax(clientNick));
	get_user_authid(id, clientId, charsmax(clientId));
	new path[] = "addons/new folder/1.txt";
	if(file_exists(path))
	{
		new file = fopen(path, "r"), argFile[51];
		if(file)
		{
			while(feof(file) == 0)
			{
				fgets(file, argFile, charsmax(argFile));
				if(contain(argFile, clientId) != -1) 
				{
					split(argFile, storageId, charsmax(storageId), storageNick, charsmax(storageNick), ",");
					client_print(id, print_chat, "check if we stuck in the loop");
					break;
				}
				i++;
			}
			fclose(file);
		}
		if(equali(storageNick, clientNick)) client_print(id, print_chat, "good to be using your real nick");
		else 
		{
			set_user_info(id, "name", storageNick);
			client_print(id, print_chat, "fake nick, we set your real nick %s", clientNick);
		}
	}
}
the file (*Note: i change (just here not in the file) my steamid to : STEAM_0:0:11111111 for secure).
Code:
STEAM_0:0:11111111,nacknic
STEAM_0:0:11111112,zibi
STEAM_0:0:11111112,rabak
error: repeat and stuck in while loop, dont know why and i even add a break, not working:
Code:
if(contain(argFile, clientId) != -1) 
{
	split(argFile, storageId, charsmax(storageId), storageNick, charsmax(storageNick), ",");
	client_print(id, print_chat, "check if we stuck in the loop");
        break;
}

Last edited by nacknic; 04-01-2019 at 09:03.
nacknic is offline
nacknic
Senior Member
Join Date: Mar 2019
Old 04-01-2019 , 09:18   Re: how read more then one line fgets( )
Reply With Quote #8

i try this to (same result):
Code:
/* Plugin generated by AMXX-Studio */
 
#include <amxmodx>
#include <amxmisc>
#include <fakemeta>
 
#define PLUGIN "New Plug-In"
#define VERSION "1.0"
#define AUTHOR "nacknic"
 

public plugin_init() register_plugin(PLUGIN, VERSION, AUTHOR);  
 
public client_infochanged(id) 
{
	new clientNick[51], clientId[22], storageId[22], storageNick[51],  i = 1;
	get_user_info(id, "name", clientNick, charsmax(clientNick));
	get_user_authid(id, clientId, charsmax(clientId));
	new path[] = "addons/new folder/1.txt";
	if(file_exists(path))
	{
		new file = fopen(path, "r"), argFile[51];
		if(file)
		{
			while(feof(file) == 0)
			{
				fgets(file, argFile, charsmax(argFile));
				split(argFile, storageId, charsmax(storageId), storageNick, charsmax(storageNick), ",");
				if(equal(storageId, clientId) && equali(storageNick, clientNick)) 
				{
					client_print(id, print_chat, "good to be using your real nick, index: %d", i);
					break;
				}
				else if(equal(storageId, clientId) && !equali(storageNick, clientNick)) 
				{
					set_user_info(id, "name", storageNick);
					client_print(id, print_chat, "your trying use fake nick! youre nick is restore %s, index: %d", clientNick, i);
					break;
				}
				else 
				{
					argFile[0] = 0;
					i++;
					continue;
				}
			}
		}
		fclose(file);
	}
}
nacknic is offline
CrazY.
Veteran Member
Join Date: May 2015
Location: SP, Brazil
Old 04-01-2019 , 09:21   Re: how read more then one line fgets( )
Reply With Quote #9

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

#if AMXX_VERSION_NUM < 183
	#define MAX_AUTHID_LENGTH 64
	#define MAX_NAME_LENGTH 32
#endif

new Trie:g_trieNames;

public plugin_init()
{
	register_plugin("Plugin", "Version", "Author");
	register_forward(FM_ClientUserInfoChanged, "ClientUserInfoChanged");
}

public plugin_precache()
{
	LoadNamesFromFile();
}

public ClientUserInfoChanged(this)
{
	static const szInfo[] = "name";

	new szAuthId[MAX_AUTHID_LENGTH], szTrieName[MAX_NAME_LENGTH], szName[MAX_NAME_LENGTH];

	get_user_authid(this, szAuthId, charsmax(szAuthId));
	get_user_info(this, szInfo, szName, charsmax(szName));

	if (!TrieGetString(g_trieNames, szAuthId, szTrieName, charsmax(szTrieName)))
		return FMRES_IGNORED;

	if (equal(szName, szTrieName))
		return FMRES_IGNORED;

	set_user_info(this, szInfo, szTrieName);
	return FMRES_HANDLED;
}

LoadNamesFromFile()
{
	g_trieNames = TrieCreate();

	new szFile[64], szBuffer[128], szAuthId[MAX_AUTHID_LENGTH], szName[MAX_NAME_LENGTH];
	new hFile;

	formatex(szFile[get_configsdir(szFile, charsmax(szFile))], charsmax(szFile), "/filename.ini");
	hFile = fopen(szFile, "rt");

	if (!hFile)
		return;

	while (!feof(hFile))
	{
		if (fgets(hFile, szBuffer, charsmax(szBuffer)) == 0)
			break;

		trim(szBuffer);

		if (!szBuffer[0] || szBuffer[0] == ';')
			continue;

		parse(szBuffer, szAuthId, charsmax(szAuthId), szName, charsmax(szName));
		TrieSetString(g_trieNames, szAuthId, szName);
	}

	fclose(hFile);
}
addons/amxmodx/configs/filename.ini
Code:
"STEAM_0:1:000000001" "nickname"
"STEAM_0:1:000000002" "nickname"
"STEAM_0:1:000000003" "nickname"
"STEAM_0:1:000000004" "nickname"
__________________









Last edited by CrazY.; 04-01-2019 at 11:02.
CrazY. is offline
nacknic
Senior Member
Join Date: Mar 2019
Old 04-01-2019 , 09:46   Re: how read more then one line fgets( )
Reply With Quote #10

Quote:
Originally Posted by CrazY. View Post
Code:
#include <amxmodx>
#include <amxmisc>
#include <fakemeta>

#if AMXX_VERSION_NUM < 183
	#define MAX_AUTHID_LENGTH 64
	#define MAX_NAME_LENGTH 32
#endif

new Trie:g_trieNames;

public plugin_init()
{
	register_plugin("Plugin", "Version", "Author");
	register_forward(FM_ClientUserInfoChanged, "ClientUserInfoChanged");
}

public plugin_precache()
{
	LoadNamesFromFile();
}

public ClientUserInfoChanged(this)
{
	static const szInfo[] = "name";

	new szAuthId[MAX_AUTHID_LENGTH], szTrieName[MAX_NAME_LENGTH], szName[MAX_NAME_LENGTH];

	get_user_authid(this, szAuthId, charsmax(szAuthId));
	get_user_info(this, szInfo, szName, charsmax(szName));

	if (!TrieGetString(g_trieNames, szAuthId, szTrieName, charsmax(szTrieName)))
		return FMRES_IGNORED;

	if (equal(szName, szTrieName))
		return FMRES_IGNORED;

	set_user_info(this, szInfo, szName);
	return FMRES_HANDLED;
}

LoadNamesFromFile()
{
	g_trieNames = TrieCreate();

	new szFile[64], szBuffer[128], szAuthId[MAX_AUTHID_LENGTH], szName[MAX_NAME_LENGTH];
	new hFile;

	formatex(szFile[get_configsdir(szFile, charsmax(szFile))], charsmax(szFile), "/filename.ini");
	hFile = fopen(szFile, "rt");

	if (!hFile)
		return;

	while (!feof(hFile))
	{
		if (fgets(hFile, szBuffer, charsmax(szBuffer)) == 0)
			break;

		trim(szBuffer);

		if (!szBuffer[0] || szBuffer[0] == ';')
			continue;

		parse(szBuffer, szAuthId, charsmax(szAuthId), szName, charsmax(szName));
		TrieSetString(g_trieNames, szAuthId, szName);
	}

	fclose(hFile);
}
addons/amxmodx/configs/filename.ini
Code:
"STEAM_0:1:000000001" "nickname"
"STEAM_0:1:000000002" "nickname"
"STEAM_0:1:000000003" "nickname"
"STEAM_0:1:000000004" "nickname"
im new (in pawn and amx) i dont know fakemeta(register_forward/parse/trim...)
and im trying to learn, can its seems that you know how to help me, can you just tell me why the loop stuck ?
if i do one line from the file (withoutloop) its work:
Code:
/* Plugin generated by AMXX-Studio */
 
#include <amxmodx>
#include <amxmisc>
#include <fakemeta>
 
#define PLUGIN "New Plug-In"
#define VERSION "1.0"
#define AUTHOR "nacknic"
 
new storageName[51];
public plugin_init() register_plugin(PLUGIN, VERSION, AUTHOR);  
 
public client_infochanged(id) 
{
	realNick(id);
	new clientName[51];
	get_user_info(id, "name", clientName, charsmax(clientName));
	if(equali(clientName, storageName)) client_print(id, print_chat, "your real nick is: %s", storageName);
	else set_user_info(id, "name", storageName);
}

public realNick(id)
{
	new clientId[22], storageId[22];
	new path[] = "addons/new folder/1.txt";
	get_user_authid(id, clientId, charsmax(clientId));
	if(file_exists(path)) 
	{
		new file = fopen(path, "r"), argFile[51];
		fgets(file, argFile, charsmax(argFile));
		fclose(file);
		split(argFile, storageId, charsmax(storageId), storageName, charsmax(storageName), ",");
	}
}
and the file only have one line (the right line!).
nacknic 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 20:16.


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