Programming Fundamentals/Loading an Array from a Text File
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.
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?
- Read the file and count the number of records.
- Create a static array of that size.
- Read the file again and add each record to the array.
- Read the file and dynamically add the records to the array.
There are two options for file processing:
- Read the entire file into memory, split the records and then process each record.
- 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 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.
Static Array Processing
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
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
- dynamic memory
- Stack created memory associated with local scope.
- static memory
- Data area memory associated with global scope.