FILES
File commands example:
'Create a file
f=OpenToWrite("Media\temp.dat")
lifes=8
WriteByte f,8
WriteShort f,lifes*9
WriteInt f, -8901
WriteFloat f,6.0
CloseFile f
'Now read it
f=OpenToRead("Media\temp.dat")
mybyte=ReadByte(f)
myshort=ReadShort(f)
myint=ReadInt(f)
myfloat=ReadFloat(f)
Text 10,10,mybyte
Text 10,30,myshort
Text 10,50,myint
Text 10,70,myfloat
CloseFile f
DrawScreen
CloseFile
Closes a previously created file.
Usage:
CloseFile file_variable
SeekFile
Change the file reading position. The first byte in a file is indexed as zero.
Usage:
SeekFile file_variable, position
StartSearch
Prepares the current working directory for file searching operations (FindFile)
EndSearch
Closes the search handle of the current directory. You should always remember to end an open search after its ready. You can search one directory at a time.
ChDir
Changes the working directory Value ".." goes one directory up. The path should not include the tailing "\"
Usage:
ChDir Path$
MakeDir
Creates a directory to the current directory. You can check if the directory was successfully created using the IsDirectory-function.
Usage:
MakeDir name$
CopyFile
Copies a file from A to B. Full filenames and paths must be specified. The target path must exist. If the target file already exists, the command will fail.
Usage:
CopyFile from$, dest$
DeleteFile
Deletes a file from disk. It will not be transferred to the "Recycle bin", so be careful.
Usage:
DeleteFile filename$
Execute
Tells Windows to open a file. The file will be opened to its associated program. For example, you can open .txt-files to Notepad and .doc-files to Microsoft Word. You can even execute http-addresses to launch a web browser. Or you can execute an email-address mailto:a@b.c which will launch your default mail application. File paths that include spaces, should be wrapped in quota (").
Usage:
Execute file$
Example:
Execute Chr(34) + "C:\Program Files\CoolBasic\versions.html" + Chr(34)
WriteByte, WriteShort, WriteInt, WriteFloat, WriteString, WriteLine
Writes a byte (1 byte), a word(2 bytes), an integer(4 bytes), a float(4 bytes), a null-terminated string or string with the line change, to a previously opened file.
Usage:
Write**** file_variable, value
OpenToRead
Opens a file for read operations only and returns its handle. If that handle is zero, opening has failed. The file must exist.
Usage:
file_variable = OpenToRead("file")
OpenToWrite
Opens a file for write operations and returns its handle. If that handle is zero, opening has failed. If the file doesn't exist, it will be created.
Usage:
file_variable = OpenToWrite("file")
OpenToEdit
Opens a file for both reading and writing. If that handle is zero, opening has failed.
Usage:
file_variable = OpenToEdit("file")
FileOffset
Tells at which position the reading/writing pointer is within a file. The starting position is at offset zero.
Usage:
offset = FileOffset(file_variable)
FindFile
Returns the name of the NEXT directory entry. Use this to extract all files and folders within a SEARCH previously opened with the StartSearch-command. Use IsDirectory to see whether it's a file or directory.
Usage:
FileName$ = FindFile()
Example:
StartSearch
Repeat
fil$=FindFile()
Print filf$
Until fil$=""
EndSearch
CurrentDir
Tells the current path (the working directory)
Usage:
dir$ = CurrentDir()
FileExists
Check if the given filename exists. Returns True if does, otherwise False (zero)
Usage:
ex = FileExists(FileName$)
IsDirectory
Tells if the given filenake turns out to be a directory. Returns 1 if directory, else zero.
Usage:
isdir = IsDirectory(fileNameAndPath$)
FileSize
Tells the size of the given file, in bytes. Directories can't be processed.
Usage:
size = FileSize(FileName$)
EOF
Indicates when there's nothing to read from a file anymore. The file must be opened first. See the example.
Usage:
endOfFile = EOF(file_variable)
Example:
While Not EOF(f)
iLine$ = ReadLine(f)
Print iLine
Wend
ReadByte, ReadShort, ReadInt, ReadFloat, ReadString, ReadLine
Read a byte (1 byte), a word (2 bytes), an integer(4 bytes) a float (4 bytes), a null-terminated string or string with the line change, from an open file.
Usage:
Read**** (file_variable)