Raised This Month: $ Target: $400
 0% 

Load ini [Lines]


Post New Thread Reply   
 
Thread Tools Display Modes
Hunter-Digital
Veteran Member
Join Date: Aug 2006
Location: In the Game [ro]
Old 08-22-2010 , 23:29   Re: Load ini [Lines]
Reply With Quote #11

A little example wouldn't hurt.

Code:
new hFile = fopen("file.txt", "rt")

if(hFile)
{
    new szRow[24] // make sure you make this bigger for longer lines

    while(fgets(hFile, szRow, charsmax(szRow)))
    {
       // szRow contains current row's text
    }

    fclose(hFile)
}
__________________

Last edited by Hunter-Digital; 08-22-2010 at 23:31.
Hunter-Digital is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 08-23-2010 , 00:36   Re: Load ini [Lines]
Reply With Quote #12

Quote:
Originally Posted by Hunter-Digital View Post
A little example wouldn't hurt.

Code:
new hFile = fopen("file.txt", "rt")

if(hFile)
{
    new szRow[24] // make sure you make this bigger for longer lines

    while(fgets(hFile, szRow, charsmax(szRow)))
    {
       // szRow contains current row's text
    }

    fclose(hFile)
}
And unfortunately that is incorrect. Afaik, fgets() does not return anything. Hence why I say to see my plugin.
__________________
fysiks is online now
nikhilgupta345
Veteran Member
Join Date: Aug 2009
Location: Virginia
Old 08-23-2010 , 00:43   Re: Load ini [Lines]
Reply With Quote #13

Yea, i've used opening and reading files before, and bugsy, in another post, told me to do this:

Code:
while(!feof(filehandler)
{
fgets(filehandler...) //dont remember other param from the top of my head
}
nikhilgupta345 is offline
Send a message via ICQ to nikhilgupta345 Send a message via Yahoo to nikhilgupta345
mottzi
Veteran Member
Join Date: May 2010
Location: Switzerland
Old 08-23-2010 , 05:38   Re: Load ini [Lines]
Reply With Quote #14

hey fysiks,

thanks first for your help.

I tried it, but i the error (Error: Must be a constant expression; assumed zero on line 46) in the RED line;
the error happends in the last code block ( get_random_maps() )

PHP Code:
#include <amxmodx>
#include <amxmisc>

new g_maps_phrases[10][65]
new 
g_maps_count

public plugin_init()
{
    
register_cvar("Bot Apology""v3.0 by Fysiks"FCVAR_SERVER|FCVAR_SPONLY)
    
register_event("DeathMsg""player_death""a","1>0")

    new 
maps[64]
    
get_configsdir(mapssizeof(maps) - 1)
    
add(mapssizeof(maps) - 1"/mapvote.ini")
    
    if( !
file_exists(maps) )
    {
        
copy(g_maps_phrases[0], 65"zm_dust2_new")
        
copy(g_maps_phrases[1], 65"zm_army_tn3_beta")
        
g_maps_count 2
        
return
    }
    
    new 
fopen(maps"rt")
    
    new 
data[65]
    
g_maps_count 0

    
while( !feof(f) && g_maps_count 65
    { 
        
fgets(fdatasizeof(data) - 1)
         
        
trim(data)
        if( !
data[0] || data[0] == ';' || data[0] == '/' && data[1] == '/' ) continue;
        
        
copy(g_maps_phrases[g_maps_count], 65data)
        
g_maps_count++
    }
    
    
fclose(f)
}

public 
get_random_maps(id)
{
    new 
random1[] = g_maps_phrases[random(g_maps_count)]  // Here <---
    
client_print(idprint_chat"%s"random1)

can you tell me why?
__________________
Quote:
#define true ((rand() % 2)? true: false) //Happy debugging suckers
mottzi is offline
Send a message via MSN to mottzi
Hunter-Digital
Veteran Member
Join Date: Aug 2006
Location: In the Game [ro]
Old 08-23-2010 , 08:29   Re: Load ini [Lines]
Reply With Quote #15

Quote:
Originally Posted by fysiks View Post
And unfortunately that is incorrect. Afaik, fgets() does not return anything. Hence why I say to see my plugin.
It returns the number of characters read on that line (including new line character, use trim() to get rid of), no point in using an extra function ( feof() ) for just that.
__________________
Hunter-Digital is offline
nikhilgupta345
Veteran Member
Join Date: Aug 2009
Location: Virginia
Old 08-23-2010 , 11:10   Re: Load ini [Lines]
Reply With Quote #16

Quote:
Originally Posted by mottzi View Post
hey fysiks,

thanks first for your help.

I tried it, but i the error (Error: Must be a constant expression; assumed zero on line 46) in the RED line;
the error happends in the last code block ( get_random_maps() )

PHP Code:
#include <amxmodx>
#include <amxmisc>

new g_maps_phrases[10][65]
new 
g_maps_count

public plugin_init()
{
    
register_cvar("Bot Apology""v3.0 by Fysiks"FCVAR_SERVER|FCVAR_SPONLY)
    
register_event("DeathMsg""player_death""a","1>0")

    new 
maps[64]
    
get_configsdir(mapssizeof(maps) - 1)
    
add(mapssizeof(maps) - 1"/mapvote.ini")
    
    if( !
file_exists(maps) )
    {
        
copy(g_maps_phrases[0], 65"zm_dust2_new")
        
copy(g_maps_phrases[1], 65"zm_army_tn3_beta")
        
g_maps_count 2
        
return
    }
    
    new 
fopen(maps"rt")
    
    new 
data[65]
    
g_maps_count 0

    
while( !feof(f) && g_maps_count 65
    { 
        
fgets(fdatasizeof(data) - 1)
         
        
trim(data)
        if( !
data[0] || data[0] == ';' || data[0] == '/' && data[1] == '/' ) continue;
        
        
copy(g_maps_phrases[g_maps_count], 65data)
        
g_maps_count++
    }
    
    
fclose(f)
}

public 
get_random_maps(id)
{
    new 
random1[] = g_maps_phrases[random(g_maps_count)]  // Here <---
    
client_print(idprint_chat"%s"random1)

can you tell me why?

If I'm not mistaken this is the problem, or part of it.

Code:
new f = fopen(maps, "rt")
Think you're making f = the directory, not the file. Don't you use fopen to open the file, and then fgets to read it? maybe make a const in the beginning to the location of the file or osmething.
nikhilgupta345 is offline
Send a message via ICQ to nikhilgupta345 Send a message via Yahoo to nikhilgupta345
tm.
Member
Join Date: Apr 2010
Old 08-23-2010 , 11:52   Re: Load ini [Lines]
Reply With Quote #17

You should try something like this for that function:
PHP Code:
public get_random_maps(id)
{
    
client_print(idprint_chatg_maps_phrases[random(g_maps_count)])

Also:
PHP Code:
copy(g_maps_phrases[g_maps_count], 65data
->
PHP Code:
copy(g_maps_phrases[g_maps_count], 64data
And everywhere else you use copy.

Last edited by tm.; 08-23-2010 at 11:55.
tm. is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 08-23-2010 , 12:18   Re: Load ini [Lines]
Reply With Quote #18

Quote:
Originally Posted by Hunter-Digital View Post
It returns the number of characters read on that line (including new line character, use trim() to get rid of), no point in using an extra function ( feof() ) for just that.
I would be nice if file.inc was documented accurately. So, there's no problem with fgets() at the end of the file? I'm assuming it just returns 0.

Quote:
Originally Posted by tm. View Post
PHP Code:
copy(g_maps_phrases[g_maps_count], 64data
And everywhere else you use copy.
Just use charsmax(g_maps_phrases) for string lengths. That way if you change array dimensions you don't have to look through and change every hardcoded constant that is based on the dimension of the array.
__________________
fysiks is online now
Vechta
Veteran Member
Join Date: Jun 2010
Old 08-23-2010 , 14:45   Re: Load ini [Lines]
Reply With Quote #19

Can you give me an example how to check if name already are in the file if loaded?

Name in file looks like this:

"Player"
"Player name 2"
"Player 3"
"Test"
Vechta is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 08-23-2010 , 16:34   Re: Load ini [Lines]
Reply With Quote #20

Quote:
Originally Posted by Vechta View Post
Can you give me an example how to check if name already are in the file if loaded?

Name in file looks like this:

"Player"
"Player name 2"
"Player 3"
"Test"
Here is one by Bugsy using SteamIDs.
__________________
fysiks is online now
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 22:43.


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