Raised This Month: $ Target: $400
 0% 

Read file


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Backstabnoob
Veteran Member
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 03-14-2011 , 09:02   Read file
Reply With Quote #1

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.
__________________
Currently busy working on a very large scale anime database project.
Backstabnoob is offline
schmurgel1983
Veteran Member
Join Date: Aug 2006
Location: Germany
Old 03-14-2011 , 09:08   Re: Read file
Reply With Quote #2

have u a sample file?
what u will reading? string num floats?
__________________

Working on:
nothing
schmurgel1983 is offline
Backstabnoob
Veteran Member
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 03-14-2011 , 11:24   Re: Read file
Reply With Quote #3

Strings.

[sample file]
de_dust2
de_cbble
de_train
[/sample file]
__________________
Currently busy working on a very large scale anime database project.
Backstabnoob is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 03-14-2011 , 13:20   Re: Read file
Reply With Quote #4

See Bot Apology in my signature for a simple example. There is also a tutorial on file reading.
__________________
fysiks is offline
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
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 15:26.


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