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

Read from a file


Post New Thread Reply   
 
Thread Tools Display Modes
shadow728988
Member
Join Date: Sep 2017
Old 07-11-2019 , 12:15   Re: Read from a file
Reply With Quote #21

Quote:
Originally Posted by OciXCrom View Post
You need to read the file to check if the SteamID is already in the list. My advice is reading the file once on map start and store all the SteamIDs in a trie, then simply check if the SteamID is in the trie when the user connects.
I wasn't the most brilliant student when they were teaching files in my C language class xD so i guess this is it for me.. thank you very much for all the help.
But still how will i check if it already exists
I open in read mode using fopen(filename, r) i guess
Then i get the contents of the file using fgets?
Then what

Last edited by shadow728988; 07-11-2019 at 12:38.
shadow728988 is offline
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 07-11-2019 , 13:59   Re: Read from a file
Reply With Quote #22

I suggest you to have a look at my plugins - I read data from an .ini file in most of them, so you can use them as an example on how to do it. This one should be most suitable for your request - https://forums.alliedmods.net/showthread.php?t=281921
__________________
OciXCrom is offline
Send a message via Skype™ to OciXCrom
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 07-11-2019 , 18:01   Re: Read from a file
Reply With Quote #23

There's really not that much to reading text files. You have to open the file with fopen in text mode and then keep reading with fgets(while(fgets()). This will give you the content of the file, one line at a time. If this is not clear, print the string you get from fgets.
If your line consists only of a steamid, now you can simply compare the steamid you are trying to add with the current line with a function like equal.
If your line contains more than a steamid, you will have to do some string processing, like breaking the string in multiple parts in order to get a string that contains only the steamid.
__________________
HamletEagle is offline
shadow728988
Member
Join Date: Sep 2017
Old 07-13-2019 , 00:33   Re: Read from a file
Reply With Quote #24

Quote:
Originally Posted by OciXCrom View Post
You need to read the file to check if the SteamID is already in the list. My advice is reading the file once on map start and store all the SteamIDs in a trie, then simply check if the SteamID is in the trie when the user connects.
Code:
#include <amxmodx>

new Trie:g_hLoggedPlayers;
new const g_szFileName[] = "NewPlayers.txt"    //FileName for the log


public plugin_init()
{
	g_hLoggedPlayers = TrieCreate();
}

public plugin_end()
{
	TrieDestroy(g_hLoggedPlayers);
}

public client_authorized(id, const authid[])
{
	if(!TrieKeyExists(g_hLoggedPlayers, authid))
	{
		logplayers(id);
		TrieSetCell(g_hLoggedPlayers, authid, 1);
	}
}

public logplayers(id)
{
	new szAuthID[MAX_AUTHID_LENGTH], szName[MAX_NAME_LENGTH];
	get_user_name(id, szName, charsmax(szName));
	get_user_authid(id, szAuthID, charsmax(szAuthID));
	log_to_file(g_szFileName, "Name %s | AuthID %s | ", szName, szAuthID);
}
is this right?
shadow728988 is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 07-13-2019 , 02:24   Re: Read from a file
Reply With Quote #25

Quote:
Originally Posted by shadow728988 View Post
Code:
#include <amxmodx>

new Trie:g_hLoggedPlayers;
new const g_szFileName[] = "NewPlayers.txt"    //FileName for the log


public plugin_init()
{
	g_hLoggedPlayers = TrieCreate();
}

public plugin_end()
{
	TrieDestroy(g_hLoggedPlayers);
}

public client_authorized(id, const authid[])
{
	if(!TrieKeyExists(g_hLoggedPlayers, authid))
	{
		logplayers(id);
		TrieSetCell(g_hLoggedPlayers, authid, 1);
	}
}

public logplayers(id)
{
	new szAuthID[MAX_AUTHID_LENGTH], szName[MAX_NAME_LENGTH];
	get_user_name(id, szName, charsmax(szName));
	get_user_authid(id, szAuthID, charsmax(szAuthID));
	log_to_file(g_szFileName, "Name %s | AuthID %s | ", szName, szAuthID);
}
is this right?
Yes.
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !

Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 07-13-2019 , 07:37   Re: Read from a file
Reply With Quote #26

It's right, but this won't stop any players from entering the server, it will only log them in the file. Also, the trie is reset when the map changes, so checking if the key exists won't actually check if the player is already in the file.

Basically the code differs from your request in the first post.
__________________
OciXCrom is offline
Send a message via Skype™ to OciXCrom
shadow728988
Member
Join Date: Sep 2017
Old 07-13-2019 , 09:35   Re: Read from a file
Reply With Quote #27

Quote:
Originally Posted by OciXCrom View Post
It's right, but this won't stop any players from entering the server, it will only log them in the file. Also, the trie is reset when the map changes, so checking if the key exists won't actually check if the player is already in the file.

Basically the code differs from your request in the first post.
Nah the initial request i dont want that now..just wanted to complete this little bit code for logging the players.. thank you
shadow728988 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 18:13.


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