AlliedModders

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

Backstabnoob 03-14-2011 09:02

Read file
 
Hello. Can anybody show me a full example of reading file fuctions? I am not nor I ever was familiar with such things in pawn, that's why I'm starting a thread here.

I need to check if current map is located in the file

ex:
the current map is dunno de_dust2

the file is like:
de_dust2
de_cbble
de_train

it is there, let the plugin work (I know how to stop it from working, I just need to see how to read a file)

If anybody wants to write that I have to create .ini file for every single map and put pluginname.amxx there then no, thanks, that's not what I'm looking for.

schmurgel1983 03-14-2011 09:08

Re: Read file
 
have u a sample file?
what u will reading? string num floats?

Backstabnoob 03-14-2011 11:24

Re: Read file
 
Strings.

[sample file]
de_dust2
de_cbble
de_train
[/sample file]

fysiks 03-14-2011 13:20

Re: Read file
 
See Bot Apology in my signature for a simple example. There is also a tutorial on file reading.

schmurgel1983 03-14-2011 13:54

Re: Read file
 
/* Reads line from file. Returns index of next line or 0 when end of file is reached. */
native read_file(const file[],line,text[],len,&txtlen);

slow :(
PHP Code:


// golbal var
new g_szSample[64][32// how many strings can be stored [1]=64, maximum string length [2]=32

public read_sample()
{
    
// get path from amxx config dir
    
new path[32]
    
get_configsdir(pathcharsmax(path)) // now config dir in path stored
    
    // store file dir in path
    
format(pathcharsmax(path), "%s/sample.txt"path// store complette path to file
    
    // check if file exists
    
if (file_exists(path))
    {
        
// do stuff
        
        // if u don't know how much lines have the file
        // use while state, i = integer, sz = string
        
new szData[33], szSample[32], iTextLengthiLine
        
        
// reads line from file. returns index of next line or 0 when end of file is reached.
        
while (read_file(pathiLineszDatacharsmax(szData), iTextLength) != 0)
        {
            
// check if more lines in the file as g_szSample[] can store...
            
if (iLine charsmax(g_szSample[]))
                break 
// this stop the while
            
            // found what on the line
            
parse(szDataszSamplecharsmax(szSample))
            
            
// now u can check, what is stored in szSample
            
            // comment or blank line = continue, read a new line!
            
if (szSample[0] == ';' || !szSample[0])
            {
                
// increase the line by 1
                
iLine++
                continue
            }
            
            
// if not comment or blank line
            // so new can new work on it
            
            // store the text in a golbal string var (g_sz)
            
g_szSample[iLine] = szSample
            
            
// increase the line by 1
            
iLine++
        }
    }


//Open a file, returns a handle or 0 on failure
native fopen(const filename[],const mode[]);

//Closes a file handle
native fclose(file);

//Returns 1 if the file is ended, 0 otherwise
native feof(file);

//Reads a line from a text file -- includes newline!
native fgets(file, buffer[], maxlength);

fast :)
PHP Code:


// golbal var
new g_szSample[64][32// how many strings can be stored [1]=64, maximum string length [2]=32

public read_sample()
{
    
// get path from amxx config dir
    
new path[32]
    
get_configsdir(pathcharsmax(path)) // now config dir in path stored
    
    // store file dir in path
    
format(pathcharsmax(path), "%s/sample.txt"path// store complette path to file
    
    // check if file exists
    
if (file_exists(path))
    {
        
// do stuff
        
        // sz = string
        
new szLineData[33], iLine
        
        
// open the file
        
new file fopen(path"rt")
        
        
// check if file valid open, if not stop here
        
if (!file) return
        
        
// file is not ended
        
while (!feof(file))
        {
            
// read one line
            
fgets(fileszLineDatacharsmax(szLineData))
            
            
// replace newlines with a null character to prevent headaches
            
replace(szLineDatacharsmax(szLineData), "^n""")
            
            
// now u can check, what is stored in szSample
            
            // comment or blank line = continue, read a new line!
            
if (szLineData[0] == ';' || !szLineData[0]) continue
            
            
// if not comment or blank line
            // so new can new work on it
            
            // store the text in a golbal string var (g_sz)
            
g_szSample[iLine] = szLineData
            
            
// increase the line by 1
            
iLine++
        }
        
        
// close the file
        
fclose(file)
    }


i hope it helps u


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

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