It is appropriate to use a sequential file structure when

Line sequential files are designed to enable you to read source or text files created with the system editor. As such, the structure is operating system dependent but typically contains variable length records with trailing spaces removed. In a line sequential file, each record in the file is separated from the next by a record delimiter.


It is appropriate to use a sequential file structure when

For files created on UNIX systems the default record delimiter is the line feed character specified by the single byte x"0A". For files created on DOS or Windows the default record delimiter is the carriage return/line feed character specified by the byte pair x"0D0A". Files created using this COBOL system on UNIX can be read on DOS and Windows, and vice versa. If you set the INSERTNULL configuration option to OFF, you must make sure that any COMP or COMP-3 data does not contain bytes with a value of x"0A" (record delimiter), x"09" (tab) or x"0D" (carriage return).

This method is the easiest method for file organization. In this method, files are stored sequentially. This method can be implemented in two ways:

1. Pile File Method:

  • It is a quite simple method. In this method, we store the record in a sequence, i.e., one after another. Here, the record will be inserted in the order in which they are inserted into tables.
  • In case of updating or deleting of any record, the record will be searched in the memory blocks. When it is found, then it will be marked for deleting, and the new record is inserted.
It is appropriate to use a sequential file structure when

Insertion of the new record:

Suppose we have four records R1, R3 and so on upto R9 and R8 in a sequence. Hence, records are nothing but a row in the table. Suppose we want to insert a new record R2 in the sequence, then it will be placed at the end of the file. Here, records are nothing but a row in any table.

It is appropriate to use a sequential file structure when

2. Sorted File Method:

  • In this method, the new record is always inserted at the file's end, and then it will sort the sequence in ascending or descending order. Sorting of records is based on any primary key or any other key.
  • In the case of modification of any record, it will update the record and then sort the file, and lastly, the updated record is placed in the right place.
It is appropriate to use a sequential file structure when

Insertion of the new record:

Suppose there is a preexisting sorted sequence of four records R1, R3 and so on upto R6 and R7. Suppose a new record R2 has to be inserted in the sequence, then it will be inserted at the end of the file, and then it will sort the sequence.

A sequential file is an ordinary text file. Each character in the file is assumed to be either a text character or some other ASCII control character such as newline. The character is in the character set specified when the file is opened. By default this is the platform-native character set.

Sequential files provide access at the level of lines or strings of text: that is, data that is not divided into a series of records. However, a sequential file is not well suited for binary data, because a number in a sequential file is written as a character string.

Opening sequential files

A sequential file can be opened in one of three modes: input, output, or append. After opening a file, you must close it before opening it in another mode.

The syntax is:

Open fileName [For {Input | Output | Append} ] As fileNumber [Len = bufferSize] [Charset = MIMECharsetName]

Where Input means read-only access to the file, Output means write-only access, and Append means write-only access starting at the end of the file. Access in all three sequential modes is one line at a time. To get an unused fileNumber, use the FreeFile function.

bufferSize is the number of characters loaded into the internal buffer before being flushed to disk. This is a performance-enhancing feature: the larger the buffer, the faster the I/O. However, larger buffer sizes require more memory. The default buffer size for sequential files is 512 bytes.

MIMECharsetName designates the character set. The default is the platform-native character set, except that if a UTF-16 or UTF-8 byte order mark (BOM) is present, the BOM character set is used, and on OS/400® the CCSID is used if a BOM is not present.

When you try to open a file for sequential input, the file must already exist. If it doesn't, you get an error. When you try to open a nonexistent file in output or append mode, the file is created automatically.

Writing to sequential files

You can write the contents of variables to a sequential file that was opened in output or append mode using the Print # or Write # statement.

The parameters to Print can be strings or numeric expressions; they are converted to their string representations automatically.

This example writes the contents of Var1 and Var2 (separated by tabs, because of the commas in the statement) to the file numbered idFile.Print#idFile, Var1, Var2

Print #idfile, Var1, Var2

The Write # statement generates output compatible with the Input # statement by separating each pair of expressions with a comma, and inserting quotation marks around strings.

For example:

Dim supV As Variant, tailV As Variant
supV = 456 
tailV = NULL
Write #idFile, "Testing", 123, supV, tailV

The statements generate the following line in the file numbered idFile:

"Testing",123,456,#NULL#

Note: True, False, and NULL are stored as strings "#True#", "#False#", and "#NULL#".

Reading from sequential files

To read data from a sequential file, open the file in input mode. Then use the Line Input # statement, the Input # statement, or the Input function, to read data from the file into variables.

Line Input # reads one line of text from a file, up to an end-of-line. The end-of-line is not returned in the string.

This example shows reading a file one line at a time until end-of-file. The Print statement displays the line and appends an end-of-line sequence.

Do Until EOF(idFile)
   Line Input #idFile, iLine
   Print iLine
Loop

Input # reads in data that was formatted and written with the Write # statement.

For example:

The file numbered idFile contains the line:

"Testing",123,456,#NULL#

Then the following statements read "Testing" into liLabel, 123 into infA, 456 into supA, and the value NULL into tailV:

Dim liLabel As String, tailV As Variant
Dim infA As Integer, supA As Integer
Input #idFile, liLabel, infA, supA, tailV

If you find that you are using Write # and Input # with sequential files often, you should consider using a random file instead. Random files are better suited for record-oriented data.

The Input function reads data from a sequential file. This function takes the number of characters to read as an argument, and returns the characters. The Input$ function returns a string to the caller. The Input function returns a Variant variable.

When would a sequential file be most appropriately used?

Sequential files are perfect for three types of persistent data: ASCII text files, "memory dumps", and stream data.

Where are sequential files used?

Sequential files are generally stored in some sorted order (e.g., alphabetic) for printing of reports (e.g., a telephone directory) and for efficient processing of batches of transactions. Banking transactions (deposits and withdrawals), for instance, might be sorted in the same order as the accounts file,…

What are the advantages of a sequential type file?

With sequential files, new data is added on to the end of the file. There are no gaps in the data. Indeed, one of the advantages of sequential files is that they are dense. That is, every byte of storage space in a sequential file is filled with a piece of information.

What are serial and sequential files and how are they used in organization?

Serial organisation is usually the method used for creating Transaction files (unsorted), Work and Dump files. Sequential files are serial files whose records are sorted and stored in an ascending or descending on a particular key field.