Reading from and writing to sequential files

EGL allows you to write and read several types of files, including indexed files, sequential files, and comma-separated value (CSV) files, also known as delimited files. These types of files differ mostly in the way they store information to the file; the process of reading from and writing to the file is similar for each type. This topic deals with sequential files and CSV files.

Prerequisites

Setting up a resource association

To connect to a file, you must first define a resource associations part that points to that file. The file itself can be a data set as well as a file on your system, but this example uses a local file.

  1. Open the project's build descriptor.
  2. Add a resource associations part to the build descriptor and open that part in the build parts editor. See "Adding a resource associations part to an EGL build file."
  3. In the new resource associations part, add a new association to the location where the sequential file will go:
    1. In the resource associations part, click the Add Association button. A new entry is displayed under Association elements.
    2. In the File Name field of the new entry, type a mnemonic for the sequential file that conforms to EGL naming requirements, such as myFile. This field is not the name of the actual file on disk; this field corresponds to the value of the fileName property of serial record parts that use this file.
    3. Set the System field to the type of system you are using, such as win for Windows® or linux for Linux®.
    4. Set the File type field to seqws to represent a sequential record.
    5. Set the systemName field to the fully qualified location of the file. For example, in a Windows operating system, systemName should be set to something like this:
      C:\myFolder\myFile.dat
      If you point to a file that does not exist, EGL will create the file when you write to it.
    6. Save and close the resource associations part.
  4. Set the value of the resourceAssociations build descriptor option to the name of the resource associations part.
  5. Define a Record part to represent the records that will be stored in the file. For example, if you want to use a sequential file, define a serialRecord:
    record mySerialRecord type serialRecord
      10 myInteger  int;
      10 myChar     char(50);
    end
  6. Set the fileName property to the value of the File Name field in the resource association part entry:
    record mySerialRecord type serialRecord
        {fileName = "myFile"}
        10 myInteger  int;
        10 myChar     char(50);
    end

Now you can use the record part in your code to access the sequential file.

For more information on resource associations parts, see the EGL Generation Guide.

Writing to the file

Writing to a file is similar to writing to any other data source.

  1. Open an EGL program or other logic part.
  2. Make sure that your serial record part is in scope and is associated with the sequential file as in "Setting up a resource association" above. You might need to use an import statement to bring it into scope:
    import myProject.myData.mySerialRecord;
  3. In an EGL program, declare a variable that is based on your serial record:
    variableRecord mySerialRecord;
  4. Add data to the fields in the new variable:
    variableRecord.myInteger = 45;
    variableRecord.myChar = "Hello!";
  5. Use an appropriate EGL data access statement, such as add to write the record to the file:
    add variableRecord;
  6. Save, generate, and run the program. The new record is written to the end of the sequential file.

Reading from the file

Reading data from a sequential or CSV file is similar to reading from any other data source, except that you must read the records in order.

In general, use the get next statement to read a single record and the get statement to read multiple records when you deal with serial records. In this context, get next begins with the first record in the sequential file and reads the records in order.

  1. Open an EGL program or other logic part.
  2. Make sure that your serial record part is in scope and is associated with the sequential file as described in "Setting up a resource association" above. You might need to use an import statement to bring it into scope:
    import myProject.myData.mySerialRecord;
  3. In an EGL logic part, declare a variable that is based on your serial record:
    variableRecord mySerialRecord;
  4. Retrieve a record from the sequential file using the variable:
    get next variableRecord;
  5. Use the data from the sequential file:
    sysLib.writeStderr(variableRecord.myChar);
  6. Save, generate, and run the program. The program reads the sequential file and displays the data from the record in the console:
    Hello!

Using CSV files

One of the advantages of CSV files is that they are human-readable. Here is a simple example of a CSV file:
123,yes,3/9/2007,Rahima
-1,no ,9/9/1999,Jorge
92,yes,,Ludmilla
This example shows three lines of data, each with four pieces of information. Each piece of information is separated by a character called a delimiter, in this case a comma. Note that the third piece of data in the third line is null, as indicated by no data between the delimiters. CSVRecord parts treat each of these pieces of information as a field. A CSVRecord for this file would look like this example:
record CsvRec type CSVRecord
{
  fileName = "CSVFile",
  delimiter = ",",
  textQualifier = "\"",
  style = CsvStyle.quoted
}

  jobCode int;
  permStatus char(3);
  hireDate date?;
  firstName string;
end
The fileName field performs the same purpose as the serialRecord; it refers to an element in a resource associations part that points to a file. However, the CSVRecord has some additional properties. The delimiter property indicates the character that separates each piece of information, in this case a comma. The textQualifier and style properties are closely related; this configuration indicates that strings in the file can be enclosed in quotes if they contain reserved characters. See CSVRecord stereotype for more information.
If you saved the example of a CSV file above to a file and created a resource associations element to point to it, you could use this record to read from and write to the file using a program like this:
program readCSV type BasicProgram {}
  
  oneRecord CsvRec;
  
  function main()
    
    //get the first record
    get next oneRecord;
    
    if (oneRecord is endOfFile)
      //if there are no records
      SysLib.writeStdout("This file is empty.");
    else
      while (oneRecord not endOfFile)
        //perform this action for each record found
        SysLib.writeStdout(oneRecord.firstName);
        get next oneRecord;
      end
    end
  end
  
end
This program reads from the file in the same way as the previous examples that used the sequential record, using the get next statement. You can write to the CSV file with the add statement in the same way.

Feedback