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

Read from .txt file anywhere?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
jonatat
Senior Member
Join Date: Dec 2017
Old 04-20-2021 , 01:44   Read from .txt file anywhere?
Reply With Quote #1

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

Last edited by jonatat; 04-20-2021 at 02:19.
jonatat is offline
jonatat
Senior Member
Join Date: Dec 2017
Old 04-20-2021 , 04:14   Re: Read from .txt file anywhere?
Reply With Quote #2

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?
jonatat is offline
jimaway
Heeeere's Jimmy!
Join Date: Jan 2009
Location: Estonia
Old 04-20-2021 , 05:45   Re: Read from .txt file anywhere?
Reply With Quote #3

Quote:
Originally Posted by jonatat View Post
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 View Post

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
jimaway is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 04-21-2021 , 01:15   Re: Read from .txt file anywhere?
Reply With Quote #4

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.
__________________
fysiks 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:26.


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