Raised This Month: $12 Target: $400
 3% 

Reading and displaying information from a file


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Hennesy
Member
Join Date: Nov 2018
Location: Czech Republic
Old 08-16-2021 , 15:45   Reading and displaying information from a file
Reply With Quote #1

Hi everyone.

Who can help, need to read configs / rules.txt file

And display in the chat every line that is in the file
__________________
I'm sorry for my english
Hennesy is offline
pr0mers
Junior Member
Join Date: May 2020
Old 08-16-2021 , 16:23   Re: Reading and displaying information from a file
Reply With Quote #2

Quote:
Originally Posted by Hennesy View Post
Hi everyone.

Who can help, need to read configs / rules.txt file

And display in the chat every line that is in the file
Code:
new String:g_sFilePath[PLATFORM_MAX_PATH];
BuildPath(Path_SM, g_sFilePath, sizeof(g_sFilePath), "configs/rules.txt"); // this is the csgo/addons/sourcemod/configs/rules.txt
new Handle:fileHandle = OpenFile( g_sFilePath, "r" );
new String:fileLine[256];
while( !IsEndOfFile( fileHandle ) && ReadFileLine( fileHandle, fileLine, sizeof( fileLine ) ) )
{
     TrimString( fileLine );
     PrintToChatAll(fileLine);
}
CloseHandle( fileHandle );
should do the trick
__________________
My Steam
My Youtube Channel
My Github
My Discord : pr0mers#0369
-I always write unnecessary plugins-
(you can tell me better ways to do something, thank you)

Last edited by pr0mers; 08-16-2021 at 16:53. Reason: added config/rules.txt
pr0mers is offline
Hennesy
Member
Join Date: Nov 2018
Location: Czech Republic
Old 08-16-2021 , 16:40   Re: Reading and displaying information from a file
Reply With Quote #3

Quote:
Originally Posted by pr0mers View Post
Code:
new String:g_sFilePath[PLATFORM_MAX_PATH];
BuildPath(Path_SM, g_sFilePath, sizeof(g_sFilePath), "configs/rules.txt"); // this is the csgo/addons/sourcemod/configs/rules.txt
new Handle:fileHandle = OpenFile( g_sFilePath, "r" );
decl String:fileLine[256];
while( !IsEndOfFile( fileHandle ) && ReadFileLine( fileHandle, fileLine, sizeof( fileLine ) ) )
{
     TrimString( fileLine );
     PrintToChatAll(fileLine);
}
CloseHandle( fileHandle );
should do the trick
need new syntax.

PHP Code:
    static char g_sFilePath[PLATFORM_MAX_PATH], fileLine[256];
    
BuildPath(Path_SMg_sFilePathsizeof(g_sFilePath), "configs/rules.txt");
    
Handle fileHandle OpenFileg_sFilePath"r" );

    while(!
IsEndOfFile(fileHandle) && ReadFileLine(fileHandlefileLinesizeof(fileLine)))
    {
        
TrimString(fileLine);
        
PrintToChatAll(fileLine);
    }

    
delete fileHandle
I'll go check
__________________
I'm sorry for my english

Last edited by Hennesy; 08-16-2021 at 16:42.
Hennesy is offline
FAQU
Member
Join Date: Sep 2020
Location: Romania
Old 08-17-2021 , 02:25   Re: Reading and displaying information from a file
Reply With Quote #4

Reading lines from a file every time you want to use them is bad practice. You should consider using a global array of strings to store the lines, and use that array later for printing.

Take a look at the way I'm doing it in my Advanced-Filters plugin: https://github.com/FAQU2/Advanced-Fi...ions.sp#L1-L60
__________________

Last edited by FAQU; 08-18-2021 at 10:37.
FAQU is offline
pr0mers
Junior Member
Join Date: May 2020
Old 08-17-2021 , 05:43   Re: Reading and displaying information from a file
Reply With Quote #5

as FAQU said something like this should be better
PHP Code:
char rules[100][258];
public 
void OnMapStart()
{
    
char g_sFilePath[PLATFORM_MAX_PATH];
    
BuildPath(Path_SMg_sFilePathsizeof(g_sFilePath), "configs/rules.txt"); // this is the csgo/addons/sourcemod/configs/rules.txt
    
Handle fileHandle OpenFileg_sFilePath"r" );
    
char fileLine[256];
    
int line 0;
    while( !
IsEndOfFilefileHandle ) && ReadFileLinefileHandlefileLinesizeoffileLine ) ) )
    {
         
TrimStringfileLine );
         
rules[line++] = fileLine;
    }
    
CloseHandlefileHandle );
}
public 
void OnPluginStart()
{
    
RegConsoleCmd("sm_rules"ruls);
}
public 
Action ruls(int client,int args){
    
int linecount sizeof(rules);
    for (
int i 0linecounti++){
        
PrintToChat(clientrules[i]);
    }


my bad ,FAQU already showed a way
__________________
My Steam
My Youtube Channel
My Github
My Discord : pr0mers#0369
-I always write unnecessary plugins-
(you can tell me better ways to do something, thank you)

Last edited by pr0mers; 08-17-2021 at 05:45.
pr0mers is offline
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 21:29.


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