AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Reading and displaying information from a file (https://forums.alliedmods.net/showthread.php?t=333909)

Hennesy 08-16-2021 15:45

Reading and displaying information from a file
 
Hi everyone.

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

And display in the chat every line that is in the file

pr0mers 08-16-2021 16:23

Re: Reading and displaying information from a file
 
Quote:

Originally Posted by Hennesy (Post 2755378)
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

Hennesy 08-16-2021 16:40

Re: Reading and displaying information from a file
 
Quote:

Originally Posted by pr0mers (Post 2755380)
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

FAQU 08-17-2021 02:25

Re: Reading and displaying information from a file
 
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

pr0mers 08-17-2021 05:43

Re: Reading and displaying information from a file
 
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


All times are GMT -4. The time now is 14:30.

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