Thread: Read file
View Single Post
schmurgel1983
Veteran Member
Join Date: Aug 2006
Location: Germany
Old 03-14-2011 , 13:54   Re: Read file
Reply With Quote #5

/* 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
__________________

Working on:
nothing
schmurgel1983 is offline