Using Data files in COBOL is done using the same techniques that have been available in COBOL for many years. Within the Form Environment Division's FILE-CONTROL Section, you code COBOL SELECT statements to select a file, assign it to an external name, and define the type of file to be used such as:
Select INFILE Assign To Myfile Organization is Sequential File Status is WS-File-Status.
In a Form Data Division's FILE Section, you code FD statements such as:
FD INFILE GLOBAL. 01 INFILE-RECORD PIC X(132).
You then use standard COBOL OPEN, READ, WRITE, CLOSE, etc. statements to access the file.
You must, however, associate the filename ("MyFile" in the select statement above) with an external filename on disk). There are three separate techniques available to accomplish this:
You can hard code a PC file name in the actual Select clause such as:
Select INFILE Assign to "c:\data\myfile.dat"
If you do not place quotes around the file name in the Select clause, you can create a data item in Working-Storage with the same name and move a PC file name into it such as:
Select INFILE Assign to WS-File-Name . . Working-Storage Section. 01 WS-File-Name Pic X(30) Value "c:\data\myfile.dat".
You can assign to a file name without quotes and instead of declaring the file name in the Working-Storage Section as in item 2 above, you can declare this dynamically at runtime by placing an entry for the module name in a COBOL85.CBR file located in the same directory as the module. For example, if the name of the module being created is "Myprog.exe", you would place the following lines of code in a COBOL85.CBR file located in the same directory as the Myprog.exe module (remember that PowerCOBOL places the Myproj.exe file in a separate subdirectory under the directory containing the project file by default):
[MYPROG] WS-File-Name=c:\data\myfile.dat
You are then free to use normal COBOL file I/O statements in your PowerCOBOL application. By adding the GLOBAL keyword to the FD clause, you may access the file from any event procedure in the same form. One event procedure may open the file, while a second event procedure reads the file, while a third event procedure writes to the file, or one event procedure may do all three.
If you add the EXTERNAL keyword to a file's FD statement, and make the identical declaration in another form or even within a separate module, you may even share the file across forms or modules.
NetCOBOL supports a special file format found in the PC environment know as LINE SEQUENTIAL. If you define a file as being LINE SEQUENTIAL in a COBOL SELECT statement, this means that each record written to the file will have all trailing spaces deleted and two bytes will be added as an end of record delimiter (Hex value X"0D0A", which represents a carriage control and line feed character).
If you are creating a file where records may have a great number of trailing spaces, this can conserve a great deal of physical space on disk. In fact, most word processors and editors store files in this format, which is sometimes called text format.
You can define a LINE SEQUENTIAL file for input or output by using the ORGANIZATION IS LINE SEQUENTIAL phrase in the file's SELECT clause.
Be careful, however that if you choose to use this format, you do not declare any non-display data types such as COMP, COMP-3 or COMP-5. The reason for this is that X"0D0A" is a valid COMP or COMP-5 value and if you are unlucky enough to have this value in a data file defined as LINE SEQUENTIAL, it will be mistakenly taken as an end of record and your record will be truncated at that point. Also note that X"09" is a tab character and can cause some undesired results as well.
A good rule of thumb is to only use the LINE SEQUENTIAL format for data files containing all PIC X and PIC 9 fields.
Note that if you code the following:
Select INFILE Assign to Myfile Organization is Sequential.
Your file will be RECORD SEQUENTIAL (no X"0D0A" will be added), which means it will be fixed length and may contain mixed data types.