Programming Fundamentals/Loading an Array from a Text File

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Overview[edit]

Loading an array from a text file requires several steps, including: opening the file, reading the records, parsing (splitting) the records into fields, adding the fields to an array, and closing the file. The file may be read all at once and then parsed, or processed line by line. The array must either be at least as large as the number of records in the file, or must be generated dynamically.

Discussion[edit]

Loading an array from a file presents an interesting dilemma. The problem resolves around how many elements you should plan for in the array. Let’s say 100, but what if the file has fewer or more than 100 values. How can the program handle it correctly?

Either:

  1. Read the file and count the number of records.
  2. Create a static array of that size.
  3. Read the file again and add each record to the array.

Or:

  1. Read the file and dynamically add the records to the array.

Processing Records[edit]

There are two options for file processing:

  1. Read the entire file into memory, split the records and then process each record.
  2. Read the file line by line and process one record at a time.

Which of these approaches is better will depend on the size of the file and the types of file and string processing supported by your programming language. Reading the entire file at once may be faster for small files. Very large files must be processed line by line.

Processing Fields[edit]

Processing fields requires splitting records based on the given file format. For example, a comma-separated-values file might be formatted as:

Celsius,Fahrenheit
0.0,32.0
1.0,33.8
2.0,35.6

The first line contains field names separated by commas. Following lines contain a value for each of the fields, separated by commas. Note that all text file input is strings. Each line must be split on the field separator (comma), and then numeric fields must be converted to integer or floating point values for processing.

Pseudocode[edit]

Static Array Processing[edit]

Open file
Read header
While Not End-Of-File
    Read line
    Increment record count
Close file

Declare array with length based on record count
Read Header
While Not End-Of-File
    Read line
    Split line into field(s)
    Convert numeric values to numeric data types
    Add field(s) to array or parallel arrays
Close file

Dynamic Array Processing[edit]

Declare empty array
Open file
Read Header
While Not End-Of-File
    Read line
    Split line into field(s)
    Convert numeric values to numeric data types
    Add field(s) to array or parallel arrays
Close file

Key Terms[edit]

dynamic memory
Stack created memory associated with local scope.
static memory
Data area memory associated with global scope.

References[edit]