Navigation:  ThinBASIC Modules > File > FILE: functions working on files >

FILE_Open

Previous pageReturn to chapter overviewNext page

 

Description

 

Prepare a file or device for reading or writing.

 

Syntax

 

n = FILE_OPEN(FileName, Mode, [RecordSize])

 

Returns

 

Number.

0 means errors.

> 0 means file number

 

Parameters

 

Name

Type

Optional

Meaning

FileName

String

No

Name of the file to be opened

Mode

String

No

Specifies the file organization and style of access (sequential, random access, or binary) for reading, writing (or both), or appending.

Posible values:

 

Mode

File type

Action

"INPUT"

Sequential

Read from

"OUTPUT"

Sequential

Write to

"APPEND"

Sequential

Append to

"BINARY"

Binary

Reading or writing

"RANDOM"

Random

Reading or writing (default)

RecordSize

Number

Yes

Len of the record or internal file buffer

 

Remarks

 

Restrictions

 

Attempting to open a file for "INPUT" that does not exist causes an Error 53 ("File not found").

Attempting to open a file that is locked can result in either an Error 70 ("Permission denied"), or an Error 75 ("Path/file access error").Similarly, attempting to OPEN a file using a file number that is already in use will result in a run-time Error 55 ("File is already open "). For this reason, programs that use hard-coded file numbers should take special care to close files before the file number is used again. In addition, code that may be used by more than one thread should use FREEFILE and avoid hard-coded file numbers.

 

If you try to open a nonexistent file for OUTPUT, APPEND, RANDOM, or BINARY operations, a new file is automatically created.

 

See also

 

File Module,

 

Examples

 

Thanks to Abraxas for the following script example

USES "FILE"

 

Dim FileHandle As DWORD

Dim Status     As DWORD

Dim TestString As String

Dim FileName   As String 

Dim sMsg       As String

 

TestString = "1234567890ABCDEF"           ' String to write to file

FileName   = APP_SCRIPTPATH + "test.txt"  ' Build filename

'

' Here are two methods of saving the test string to a file

'

' First method

'

' Limitations - it will only write what is in the SringBuffer

' Advantages - Handles opening/closing the file

'

Status     = FILE_SAVE(FileName, TestString)   ' Save string to File

'

' Second method

'

' Limitations - None (that i can think of)

' Advantages - You can add and remove data

'

' The seek function can be used to move around the file to set the read / write position

'

FileHandle = FILE_Open (FileName, "BINARY")     ' Open file for writing

Status     = FILE_PUT  (FileHandle, TestString) ' Write String to File

Status     = FILE_Seek (FileHandle,  5)         ' Set the current write position to 5th byte in the file

Status     = FILE_PUT  (FileHandle, TestString) ' Write String to File

Status     = FILE_Close(FileHandle)             ' Release File

'

' Now lets try and read some bytes back into memory

' Lets build the string '166'

'

sMsg        = "Bytes read from file are " + $DQ

FileHandle  = FILE_Open(FileName, "BINARY"' Open file for reading

Status      = FILE_Seek(FileHandle,  1)     ' Set the current read position to 1st byte in the file

sMsg       += FILE_Get (FileHandle,  1)     ' Read 1 byte and add to output message

Status      = FILE_Seek(FileHandle, 10)     ' Reset the current File read position to 10th byte in the file

sMsg       += FILE_Get (FileHandle,  1)     ' Read 1 byte and add to output message

Status      = FILE_Seek(FileHandle, 10)     ' File read position has moved to the next byte so we need to Reset to 10th byte in the file

sMsg       += FILE_Get (FileHandle,  1)     ' Read 1 byte and add to output message

Status      = FILE_Close(FileHandle)        ' Release File

 

sMsg       += $DQ + $CRLF + $CRLF

sMsg       += "File Contents " + $DQ

sMsg       += FILE_LOAD(FileName)           ' Read File into Ouput string buffer

sMsg       += $DQ

 

MSGBOX 0, sMsg   ' Output string buffer