Files are used to save and retrieve data to and from a disk, flash or similar device. To locate a file, you must provide a "path" to that file. The first item in the path is the device, followed by a list of folders, and ending with the file name. The device name, folder names and file name are separated by "/" characters. For example:
/ROMDISK/my_folder/my_file.dat
File names often contain an embedded "." followed by a character sequence that indicates the file type. This file type is treated as simply part of the file name and is ignored by the file system. However, the file type is used by certain system components. For example, the GPL compiler assumes that source files always have the type ".gpl", so a file name might be:
/flash/projects/Myproject/Main.gpl
Files may be either temporary or permanent. Temporary files are written to a temporary memory-based disk with device name "/ROMDISK". These files consume blocks of the CPU's main memory and are lost when the controller is restarted. Temporary files may be read or written very quickly. Permanent files are written to a disk that is part of the non-volatile flash memory, with device name "/flash". This disk is very slow to write, but may be read quickly. All file paths must begin with either "/ROMDISK" or "/flash".
If your /flash device has been divided into sub-partitions, you can use the devices /flash2, /flash3, , /flash8, depending on how many sub-partitions have been created.
Follow this procedure:
| Step | Action |
|---|---|
|
1. |
Open the file by creating a StreamWriter object. The path to the file to be written is a required argument to the StreamWriter New method. If you want to append to an existing file, set the append input parameter to True. Otherwise a new file is created, overwriting any existing file that matches the path. |
|
2. |
Decide if you want your data to be buffered during output. If not, change the AutoFlush property to True, from its default value of False. Setting AutoFlush to True will make the output much slower, especially for the /flash device. |
|
3. |
If you are going to organize your output data into lines, decide if the default line terminator (CR-LF) is appropriate. If not, use the NewLine property to change it. |
|
4. |
Use the Writeor WriteLine methods to write the data. |
|
5. |
Use the Close method to force any pending output to be written and to update all internal file data. |
Follow this procedure:
| Step | Action |
|---|---|
|
1. |
Open the file by creating a StreamReader object. The path to the file to be read is a required argument to the StreamReader New method. |
|
2. |
Read the data by using either the Read or ReadLine methods. |
|
3. |
Use the Peek method to check for the end of the file, or enclose the read operation in a Try-Catch block to capture read errors. |
|
4. |
Use the Close method to release any system resources held by the object. |
In this example, a temporary file is created using the StreamWriter object "o" and written with lines that contain the string values "Line 1" through "Line 10". The file is closed and then opened for read using the StreamReader object "i". If the Peek method does not indicate end-of-file, a line is read from the input file and written to the console. Finally the input file is closed.
Public Sub file_write_read
' Write a file, read it back, and display it on the console
Dim o As New StreamWriter("/ROMDISK/filetest")
Dim i As StreamReader
Dim line As String
Dim ii As Integer
For ii = 1 To 10
o.WriteLine("Line " & CStr(ii))
Next ii
o.Close()
i = New StreamReader("/ROMDISK/filetest")
While i.Peek() >=0 ' Check if end-of-file
line = i.Readline()
Console.WriteLine(line)
End While
i.Close()
End Sub