faq | docs | tutorials | guide | reference | contact | home  

TUTORIAL - FILES Previous | Next | Tutorial Home

The FGL programming environment makes it easy to interact with low-level files and other system resources through integrated functions and classes. Virtually any type of file can be accessed, whether binary, ASCII text, database, or otherwise.

Generally, FGL puts low-level files into one of three categories: binary, ASCII text, or INI file – which is a special instance of ASCII text. The simplest is the reading and writing of ASCII text files, for example:

Simple ASCII file example:
<[
    // specify a file
    fname = 'c:\fgl\readme.txt'
]>
<html>
<head>
 <title>Simple ASCII file example</title>
</head>
<body text=black bgcolor=white>

<!-- display the file contents -->
<p><[ ! fname ]><hr><br>
<pre><[ ! fileReadASCII( fname ) ]></pre>

</body>
</html>

INI files are standard ASCII text configuration files that consist of name/value pairs within individual sections. FGL provides function to access and interact with INI files, for example:

Simple INI file example:
<[
    // specify a file
    fname = 'c:\fgl\server.ini'
]>
<html>
<head>
 <title>Simple INI file example</title>
</head>
<body text=black bgcolor=white>
<[
    ! "Server Name: " + iniGetString( fname, "CONFIG", "SERVER_NAME", "" )
    ! "<br><br>"
    ! "Config Keys: " + iniGetKeys( fname, "CONFIG" )
]>
</body>
</html>

FGL’s object support can be used to create wrappers around related data and functions. This can be used to enhance the INI file interaction, as in the following example (NOTE: the "objIniFile" class is included in the SUPPORT.FLB file:

Advanced INI file with object wrapper example:
<[
    CLASS inifile
      PUBLIC:
        local fname
        
        METHOD new( fname )
            ::fname = fname
            return( 1 )
        END
        
        METHOD get( section, item, defval )
            return( iniGetString( ::fname, section, item, defval ) )
        END
        
        METHOD set( section, item, val )
            return( iniSetString( ::fname, section, item, val ) )
        END
        
        METHOD delete( section, item )
            return( iniSetString( ::fname, section, item, null ) )
        END
        
        METHOD GetKeys( section )
            return( iniGetKeys( ::fname, section ) )
        END
        
        METHOD aGetKeys( section )
            // get keys as an array
            return( split( ::getkeys( section ), ";" ) )
        END
        
        ACCESS size
            return( filesize( ::fname ) )
        END
        ACCESS timestamp
            return( filedate( ::fname ) + " " + filetime( ::fname ) )
        END
    END
    
    // create an instance of the class
    ini = new( "inifile", 'c:\fgl\data\mydata.ini' )
]>
<html>
<head>
 <title>Advanced INI file with object wrapper example</title>
</head>
<body text=black bgcolor=white>
<[
    ini.set( "config", "date", date( ) )
    ini.set( "config", "time", time( ) )
    ini.set( "config", "day", date( "DDDD" ) )
    ini.set( "config", "month", date( "MMMM" ) )
    
    ! ini.fname + ", " + ini.size + " bytes, dated " + ini.timestamp
    ! "<br><br>"
    
    a = ini.aGetKeys( "config" )
    cnt = len( a )
    for ( i=1; i<=cnt; i++ )
        ! a[i] + " = " + ini.get( "config", a[i], "" ) + "<br>"
    end
]>
</body>
</html>

FGL’s low-level file support includes a number of functions for the direct manipulation of virtually any type of file including binary files through the use of file handle references. FGL includes more than 50 low-level file functions for the advanced creation and interaction of low-level files. Consult the FGL reference guide for details. The following is an example of low-level file manipulation:

Advanced Low-level file example:
<[
    // specify a file
    fname = 'c:\fgl\readme.txt'
]>
<html>
<head>
 <title>Advanced Low-level file example</title>
</head>
<body text=black bgcolor=white>
<[
    // open the file in read-only mode and get a file handle
    hfile = fileOpen( fname, 0 )
    if ( hfile < 1 )
        return( "Error trying to open file (" + fname + ")" )
    end
    
    i=1
    // read file one line at a time and add line numbers
    while ( ! feof( hfile ) )
        ! "<font color=#FF8080>" + right( "000" + i, 3 ) + "</font> &nbsp; "
        ! freadline( hfile ) + "<br>\r\n"
        i++
    end
    
    // close the file
    fileClose( hfile )
]>
</body>
</html>

#####