Files
This chapter describes simple, textual or binary files and how to
create and use them. Php is largely used to manage databases and it
handles lot of formats, but this is outside the goal of this tutorial,
the SQL and other data access libraries have to be studied in the Php
manual.
Scriptol has a particular scalar type for file management: file,
that has methods.
Creating
file is created by the "open" command. The access mode for a file is the second parameter of the "open" method.
file myfile ` creates an instance of the virtual class file myfile.open("name", "mode") ` opens the file error ? print "non ouvert" ` tests for error.
The name is either a local filename or an URL starting with ftp://
or http://
(for Scriptol-Php only).
File acces modes
"r"
"w" "r+" "a" "a+" |
read, from the start. write, at the begining. read or write at start. append, write at end . read at current position, or write at end. |
Once a file is open, the position to read is either the beginning of
the file, or any position given by the "seek" method.
Example:
myfile.seek(250)
... read or write skipping the first 250 bytes.
Seek works only with a local file.
To test if the file is found or accessible, one uses a hidden flag that
is available through the "error" control structure.
Reading
A file may be read line per line, or by binary blocks.
In the first case, the readLine() method is used, otherwise the read
method has the size of the block as parameter.
Example:
text t = readLine() ` reading a line of text
The end of line code is included in the text and may be removed by the rTrim() text method.
Other example:
text t = read(1000) ` reading 1000 bytes
Writing
The write method puts the content of a text variable inside a file.
Once the file is open (either in "w" or "a" mode), and thus written
either from scratch or at end of the current content, each new call
to the write method results in appending a new text at end.
Functions | |
file fopen(text, text) text rTrim(text) boolean file_exists(text) number filesize(text) |
Creates or opens a file according to
name and mode Removes the end of line code. Returns true if the file exists, false otherwise. Returns the size of the file, the name in parameter. |
Methods of the file virtual classe | |
void open(text, text) void close() text read(int) int write(text) text readLine() int seek(int) boolean file_eof() |
Creates or opens a file according the
name and mode. Closes the file. Reads n bytes in parameter from the file. Adds a text to the file, according the mode. Reads the next line from a text file. Points out the location in parameter. Returns true at end of file. |
Example of sequential writing into a file. | text
fname = "name" `
file name file out ` name of the file objectl out.open(nomfic, "w") ` opened to read error ? die("impossible d'ouvrir " + nomfic) for text line in mylist ` some array out.write(line + "\n") ` store next element /for out.close() ` closes the file |
Exercises |
1) Program the reading of a text file named "myfile.txt",
and display the content. |