You use PowerScript text file functions to read and write text in line mode or stream mode, or to read and write binary files in stream mode.
The FileOpen function can open Unicode and ANSI files. If the file does not exist, FileOpen creates a Unicode file. The FileClose function saves the file in the format in which it was opened.
In line mode, the FileRead, FileReadEx, FileWrite, and FileWriteEx functions can read and write to Unicode files.
You can read a file a line at a time until either a carriage return or line feed (CR/LF) or the end-of-file (EOF) is encountered. When writing to the file after the specified string is written, PowerScript appends a CR/LF.
In stream mode, the FileRead, FileReadEx, FileWrite, and FileWriteEx functions can read and write to Unicode and ANSI files. FileOpen and the file read and write functions assume that any file is a binary file. FileOpen opens the file as a binary file; FileWrite and FileWriteEx writes to it as a binary file.
You can read the entire contents of the file, including any CR/LFs. When writing to the file, you must write out the specified string (but not append a CR/LF).
The format in which FileWrite and FileWriteEx append data to a file depends on the format of the data, not the format of the file. If you append a string entered in PocketBuilder to an ANSI file, it is written as a Unicode character string. If you are reading from or writing to ANSI files in stream mode, use the FromANSI and ToANSI functions to convert ANSI blobs to Unicode character strings, or Unicode characters to ANSI blobs.
This code opens an ANSI file in stream mode, reads the data into a blob, then converts the blob into a Unicode string:
long ll_fnum integer li_bytes string ls_unicode blob lb_ansill_fnum = FileOpen("employee.dat",StreamMode!, Read!, LockWrite!, Replace!)li_ bytes = FileRead(ll_fnum, lb_ansi) ls_unicode = FromANSI(lb_ansi) FileClose(ll_fnum)
This code converts a Unicode character string into an ANSI blob, opens an ANSI file in stream mode, and writes the blob to the file:
lb_ansi = ToANSI(ls_unicode) ll_fnum = FileOpen("employee.dat",StreamMode!, Write!, LockWrite!, Replace!)li_ bytes = FileWrite(ll_fnum, lb_ansi) FileClose(ll_fnum)
If the file is in Unicode format and has the two-byte flag, use the Mid function to skip the leading bytes when the file is opened in stream mode:
bytes = fileread(ll_fnum, ls_unicode) ls_string = Mid(ls_unicode, 2)
Reading a file into a MultiLineEdit You can use stream mode to read an entire file into a MultiLineEdit, and then write it out after it has been modified.
When PocketBuilder opens a file, it assigns the file a unique integer and sets the position pointer for the file to the position you specify (the beginning or end of the file). You use the integer to identify the file when you want to read the file, write to it, or close it. The position pointer defines where the next read or write will begin. PocketBuilder advances the pointer automatically after each read or write.
You can also set the position pointer with the FileSeek function.
Table 19-3 lists the built-in PowerScript functions that manipulate files.
Function |
Datatype returned |
Action |
---|---|---|
FileClose |
Integer |
Closes the specified file |
FileDelete |
Boolean |
Deletes the specified file |
FileExists |
Boolean |
Determines whether the specified file exists |
FileLength |
Long |
Obtains the length of the specified file |
FileOpen |
Integer |
Opens the specified file |
FileRead and FileReadEx |
Integer |
Read from the specified file |
FileSeek |
Long |
Seeks to a position in the specified file |
FileWrite and FileWriteEx |
Integer |
Write to the specified file |