GLBasic User Manual

Main sections

OPENFILE()

ok = OPENFILE(channel%, file$, mode%)



Opens the file file$ for either writing (mode%=FALSE or 0, or -1 to append to the file) or reading (mode%=TRUE or 1) and assigns the channel index channel% to it. The return value is either TRUE or FALSE indicating whether the file could be opened.

mode%:
-1 - append to file
0 - write
1 - read


<B>Reading from Files</B>


READBYTE channel%, val%%
READUBYTE channel%, val%%
READWORD channel%, val%%
READUWORD channel%, val%%
READLONG channel%, val%%
READULONG channel%, val%%
READIEEE channel%, val##
READSHORTIEEE channel%, val##
READSTR channel%, val$#, nc%
READLINE channel%, val$#



<B>Writing to Files</B>


WRITEBYTE channel%, val%
WRITEWORD channel%, val%
WRITELONG channel%, val%
WRITEUBYTE channel%, val%
WRITEUWORD channel%, val%
WRITEULONG channel%, val%
WRITEIEEE channel%, val#
WRITESHORTIEEE channel%, val#
WRITESTR channel%, val$
WRITELINE channel%, val$



BYTE = 1 Byte, (0..255)
WORD = 2 Byte, (0..65535)
U = without sign. (e.g. 0..255 instead of -127..128 for BYTE)
LONG = 4 Byte, (0..4294967295)
IEEE = 8 Byte, IEEE 64 Bit floating point number
SHORTIEEE = 4 Byte, IEEE 32 Bit floating point number

Integer numbers will be loaded without sign (unsigned). Thus the value -127 will be BYTE 255.

With IEEE you can load floating point numbers. This is the best way to store GLBasic numbers.

The value nc% for READSTR specifies how many characters (BYTE) to read into the string.

READLINE can be used with files produced on both Unix-type systems (lines end with only a new line character) or non-Unix systems (lines end with a carriage-return/line feed sequence).
Data will be read until a "\n" (newline) occurrence in the file. A carriage return ("\r") on the line will be trimmed if it exists.
When writing, WRITELINE will put append the carriage-return/line feed sequence ("\r\n") to the line. If you want to write UNIX format, use:
WRITESTR 1, text$ + "\n"


Example
// Files
test$ = "test.bin"
OPENFILE(1, test$, FALSE)

WRITEBYTE 1, 42
WRITEBYTE 1, -42
WRITEWORD 1, 16767
WRITEWORD 1, -16767
WRITEIEEE 1, 1.234E-4

xx$= "Hello World\nYes, Hello\n"
WRITESTR 1, xx$
WRITELONG 1, 16767
WRITELONG 1, -16767
CLOSEFILE 1

LOCAL b1%, b2%, w1%, w2%, l1%, l2%, ieee
LOCAL x2$, _x1$, _x2$
OPENFILE(1, test$, TRUE)
READBYTE 1, b1
READBYTE 1, b2
READWORD 1, w1
READWORD 1, w2
READIEEE 1, ieee

// READSTR 1, x2$, LEN(xx$)
READLINE 1, _x1$
READLINE 1, _x2$
READLONG 1, l1
READLONG 1, l2
CLOSEFILE 1

LOCAL i%
PRINT "b1="+b1, 0,i); INC(i, 10);
PRINT "b2="+b2, 0,i); INC(i, 10);
PRINT "w1="+w1, 0,i); INC(i, 10);
PRINT "w2="+w2, 0,i); INC(i, 10);
PRINT "ie="+ieee, 0,i); INC(i, 10);
PRINT "st="+_x1 + "-" + _x2, 0,i); INC(i, 10);
PRINT "l1="+l1, 0,i); INC(i, 10);
PRINT "l2="+l2, 0,i); INC(i, 10);

SHOWSCREEN
MOUSEWAIT




OPENFILE(1, "test.txt", FALSE)
WRITELONG 1, 1234
WRITELONG 1, 5678
WRITELONG 1, 9911
CLOSEFILE 1

LOCAL i

// 1234
OPENFILE(1, "test.txt", TRUE)
DEBUG "pos(0)="+FILEPOSITION(1)+"\n"
READLONG 1,i%; DEBUG "read="+i+"\n"

// 1234
FILESEEK(1, 0, 0)
DEBUG "pos(0)="+FILEPOSITION(1)+"\n"
READLONG 1,i%; DEBUG "read="+i+"\n"

// 1234
FILESEEK(1, -4, 1)
DEBUG "pos(0)="+FILEPOSITION(1)+"\n"
READLONG 1,i%; DEBUG "read="+i+"\n"

// 1234
FILESEEK(1, -12, -1)
DEBUG "pos(0)="+FILEPOSITION(1)+"\n"
READLONG 1,i%; DEBUG "read="+i+"\n"

// 9911
FILESEEK(1, 4, 1)
DEBUG "pos(0)="+FILEPOSITION(1)+"\n"
READLONG 1,i%; DEBUG "read="+i+"\n"

CLOSEFILE 1

See also...