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 the file and writing to the file is similar for each type. This topic deals with sequential files and CSV files.
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.
C:\myFolder\myFile.dat
If
you point to a file that does not exist, EGL will create the file
when you write to it.The resource associations part looks like this, with your own values in the fields:

record mySerialRecord type serialRecord
10 myInteger int;
10 myChar char(50);
end
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 a file is similar to writing to any other data source.
import myProject.myData.mySerialRecord;
variableRecord mySerialRecord;
variableRecord.myInteger = 45;
variableRecord.myChar = "Hello!";
add variableRecord;
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.
import myProject.myData.mySerialRecord;
variableRecord mySerialRecord;
get next variableRecord;
sysLib.writeStderr(variableRecord.myChar);
Hello!
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.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.