AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Read from .txt file anywhere? (https://forums.alliedmods.net/showthread.php?t=332042)

jonatat 04-20-2021 01:44

Read from .txt file anywhere?
 
Hello guys. Im tryingo todo to read .txt file in anywhere. My code:

PHP Code:

stock GetMatchID()
{
new 
fp fopen("addons/amxmodx/configs/gunid.txt"), gettheid[100];
while (!
foef(fp));
{
new 
showmatchid fgets(fpgettheidcharsmax(gettheid);
return 
gettheid;
}
fclose(fp)


But getting error then compiling. Why i'm using stock? I want to show the ID anywhere where i put %s. Example:

PHP Code:

client_printcolor(0"Gun ID is now: %s"GetMatchID


jonatat 04-20-2021 04:14

Re: Read from .txt file anywhere?
 
Ok so i figgured out:

PHP Code:

stock GetMatchID()
{
new 
szDirectory128 ];
get_configsdirszDirectorycharsmaxszDirectory ) );
addszDirectorycharsmaxszDirectory ), "/gameid.txt" );

new 
size file_sizeszDirectory

new 
szLine128 ], iLen;

for ( new 
size i++ )
{
read_fileszDirectoryiszLinecharsmaxszLine ), iLen );
}
return 
szLine


But now, i want to insert into database like that:

PHP Code:

public StartGameLIVE()
{
new 
szQuery[3800];
new 
map_name[64];

get_mapname(map_name63)

formatex(szQuery3799"INSERT INTO `matches` (`match_id`, `map`, `played_at`, `left_score`, `right_score`, `state`, `server`, `left_team_id`, `right_team_id`, `game_id` ) VALUES ( '%s', '%s', NOW(), '0', '0', '0', '85.566.954.58', '1', '1', '3' );"GetMatchID(), map_name); 
SQL_ThreadQueryg_hTuple"QuerySetData"szQuery);

return 
PLUGIN_CONTINUE;


But getting error on that SQL line. Any ideas?

jimaway 04-20-2021 05:45

Re: Read from .txt file anywhere?
 
Quote:

Originally Posted by jonatat (Post 2744590)
But now, i want to insert into database like that:

PHP Code:

formatex(szQuery3799"INSERT INTO `matches` ...... %s', '%s', .......' );"GetMatchID(), map_name); 

But getting error on that SQL line. Any ideas?

formatex() is expecting a string but GetMatchID() returns an integer

Quote:

Originally Posted by jonatat (Post 2744582)

But getting error then compiling. Why i'm using stock? I want to show the ID anywhere where i put %s. Example:

PHP Code:

client_printcolor(0"Gun ID is now: %s"GetMatchID


you should store the file contents if you're using it a lot. reading the file to get the same value over and over again is a bad idea

fysiks 04-21-2021 01:15

Re: Read from .txt file anywhere?
 
You should avoid using read_file() as it is not recommended. Also, you should generally never return a string from a function.

What exactly is this data from the file you're trying to read and what does the file look like? I don't think your code is working the way you think it is.

Also, for reading the lines of a file correctly, you should do this:
PHP Code:

new fszBuffer[100]

fopen("file.txt""rt")

if( 
)
{
    while( 
fgets(fszBuffercharsmax(szBuffer)) )
    {
        
trim(szBuffer)

        
// szBuffer now contains the contents of one
        // line of the file without the newline or
        // carriage return.
    
}
    
fclose(f)


If you only need to read the first line then you can simply omit the while loop entirely and just call fgets() once.


All times are GMT -4. The time now is 02:38.

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