1. How do I tell whether a file exists or not?
When writing software one of the basic tasks is checking whether files exist and how big they are. In Ubercode these tasks are achieved with the
Filesize function. This returns -1 if the file is not there, or if the file exists it returns a value >= 0 which is the size of the file in bytes.
You do not need to trap any exception codes to use Filesize. For example:
// Fsize.cls
Ubercode 1 class Fsize
public function main()
var
filename : string[*]
size : integer(-1:MAXINT)
code
filename <- Inputbox("Filesize", "Enter file name")
size <- Filesize(filename)
select size
case -1 => call Msgbox("Filesize", filename + ": not there")
case 0 => call Msgbox("Filesize", filename + ": is empty")
else => call Msgbox("Filesize", filename + ": " + Str(size) + " byte(s)")
end select
end function
end class
2. How do I delete a file?
Use the Filedel function. For example:
// Fdel.cls
Ubercode 1 class Fdel
public function main()
var
filename:string[*] // single file name
code
filename <- Openfiledialog("Delete file", 0, Dirpath() + "*.*")
if filename /= "" and
Msgbox("Delete?", filename, "Yes" + NL + "No") = "Yes" then
call Filedel(filename)
end if
end function
end class
If you call Filedel when the file does not exist, no error occurs and nothing happens. The purpose of Filedel is to remove the file, and this
purpose is met if the file never existed in the first place. Use Filesize if you want to know whether a file exists or not.
3. How do I copy a file?
Use the Filecopy function which copies a source file to a destination file. The source file should exist and the destsination file should not exist
(if it does, it will be overwritten). The following example shows how to use Filecopy:
// Fcopy.cls
Ubercode 1 class Fcopy
public function main()
var
source : string[*] // source file
dest : string[*] // destination file
code
source <- Openfiledialog("File to copy", 0, "*.*")
if source /= "" then
dest <- Saveasdialog("Destination file", 0, "*.*")
if dest /= "" and source /= dest then
call Filecopy(source, dest)
call Msgbox("Filecopy", Str(Filesize(dest)) + " byte(s)")
end if
end if
end function
end class
The program works by prompting for source which is the file being copied, then prompting for dest which is the name to be given to the copy.
If the filenames were entered correctly and the source file exists, then Filecopy is used to copy the file. After copying the file, the Msgbox
shows the number of bytes that were copied.
Filecopy also works with files having a size of zero bytes. If a file of size zero is copied, the resulting destination file will also have a size of
zero bytes.
4. How do I edit the lines in a text file?
If you only want to edit part of the file, perhaps because the file has a special structure, read in the part of the file you want and display it in a multi
line Edit object, or using an Editwindow object. After making changes merge the part back into the file.
If you want to edit all the file, use Loadtext to read it into an EditWindow, edit it, then call Savetext to write the changed text back out
to disk. The Sdi example program shows how this is done.
5. How do I loop through a file containing binary data?
The data needs to have a fixed record size, and you need to know the layout of the data. You may be able to work this out by examining the data file with a
debugger. Declare a record that maps onto the file records, and at run time use use Sizeof to check the record size matches the file size. Then declare
an external list variable with the record as a component, then loop through the file from record 1 to the last record. Use Listread / Listwrite to read
and write the records.
|