Open File statement - MapBasic - 2023

MapInfo MapBasic Reference

Product type
Software
Portfolio
Locate
Product family
MapInfo
Product
MapInfo > MapBasic
Version
2023
Language
English
Product name
MapBasic
Title
MapInfo MapBasic Reference
First publish date
1985
Last updated
2023-09-12
Published on
2023-09-12T16:32:32.686312

Purpose

Opens a file for input/output.

Syntax

Open File filespec
	[ For { Input | Output | Append | Random | Binary } ]
	[ Access { Read | Write | Read Write } ]
	As [ # ] filenum 
	[ Len = recordlength ]
	[ ByteOrder { LOWHIGH | HIGHLOW } ]
	[ CharSet char_set ]

filespec is a string representing the name of the file to be opened.

filenum is an integer number to associate with the open file; this number is used in subsequent operations (for example, Get statement or Put statement).

recordlength identifies the number of characters per record, including any end-of-line markers used; applies only to Random access.

char_set is the name of a character set; see CharSet clause.

Restrictions

You cannot issue an Open File statement through the MapBasic window.

Description

The Open File statement opens a file, so that MapBasic can read information from and/or write information to the file.

In MapBasic, there is an important distinction between files and tables. MapBasic provides one set of statements for using tables (for example, Open Table statement, Fetch statement, and Select statement) and another set of statements for using other files in general (for example, Open File, Get statement, Put statement, Input # statement, Print # statement).

The For clause specifies what type of file i/o to perform: Sequential, Random, or Binary. Each type of i/o is described below. If you omit the For clause, the file is opened in Random mode.

Sequential File I/O

If you are going to read a text file that is variable-length (for example, one line is 55 characters long, and the next is 72 characters long, etc.), you should specify a Sequential mode: Input, Output, or Append.

If you specify the For Input clause, you can read from the file by issuing an Input # statement and a Line Input # statement.

If you specify the For Output clause or the For Append clause, you can write to the file by issuing a Print # statement and a Write # statement.

If you specify For Input, the Access clause may only specify Read; conversely, if you specify For Output, the Access clause may only specify Write.

Do not specify a Len clause for files opened in any of the Sequential modes.

Random File I/O

If the text file you are going to read is fixed-length (for example, every line is 80 characters long), you can access the file in Random mode, by specifying the clause: For Random.

When you open a file in Random mode, you must provide a Len = recordlength clause to specify the record length. The recordlength value should include any end-of-line designator, such as a carriage-return line-feed sequence.

When using Random mode, you can use the Access clause to specify whether you intend to Read from the file, Write to the file, or do both (Read Write). After opening a file in Random mode, use the Get statement and the Put statement to read from, and write to, the file.

Binary File I/O

In Binary access, MapBasic converts MapBasic variables to binary values when writing, and converts from binary values when reading. Storing numerical data in a Binary file is more compact than storing Binary data in a text file; however, Binary files cannot be displayed or printed directly, as can text files.

To open a file in Binary mode, specify the clause: For Binary.

When using Binary mode, you can use the Access clause to specify whether you intend to Read from the file, Write to the file, or do both (Read Write). After opening a file in Binary mode, use the Get statement and the Put statement to read from, and write to, the file.

Do not specify a Len clause or a CharSet clause for files opened in Binary mode.

Controlling How the File Is Interpreted

The CharSet clause specifies a character set. The char_set parameter should be a string constant, such as "WindowsLatin1". If you omit the CharSet clause, MapInfo Pro uses the default character set for the hardware platform that is in use at run-time. Note that the CharSet clause only applies to files opened in Input, Output, or Random modes. See CharSet clause for more information.

If you open a file for Random or Binary access, the ByteOrder clause specifies how numbers are stored within the file.

If your application only runs on one hardware platform, you do not need to be concerned with byte order; MapBasic simply uses the byte-order scheme that is "native" to that platform. However, if you intend to read and write binary files, and you need to transport the files across multiple hardware platforms, you may need to use the ByteOrder clause.

Examples

Open File "cxdata.txt" For INPUT As #1
Open File "cydata.txt" For RANDOM As #2 Len=42
Open File "czdata.bin" For BINARY As #3

See Also:

Close File statement, EOF() function, Get statement, Input # statement, Open Table statement, Print # statement, Put statement, Write # statement, CharSet clause