Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading from a text file

Status
Not open for further replies.

driggels

Programmer
Jul 11, 2001
15
0
0
US
I need to read from a text file. The file is a series of different records, each record with specific fields defined. The records are distinguished by the 3 and 4 characters of the record. I tried to use a FILEDEF statement to specify where the file was located, but for some reason I can't seem to connect to the file. Depending on how I try to read the file (with Dialog Manager or with a TABLE FILE ... statement) I either get an error saying there are no records on the file (whether or not I actually point to the file) or I get an error message saying the file is not found (again, whether or not I point to the actual file). Has anyone ever done this? Do you have an example?
 
To better assist, I need an example of the data. There are two ways to handle reading the file.

SAMPLE DATA:

test.dat

1abc9999999999
20000aaaaaaaaaaaa
1cdf8888888888
29393llllllllllll
1dkd8383838383
1iej8383838281
29342oweselcoekco

As you can see there are two different records within this table. The 1st column distinguishes them.


1st method.

Create a master file description for the file.
The mfd can contain one field that has a format of Axx where xx is the max length of the field. Then you filedef the file and then you can read each record in and parse the record into fields. Example

tofile.mas

FILENAME=TOFILE, SUFFIX=FIX
SEGNAME=TOFILE, SEGTYPE=S
FIELD=TODATA, ALIAS=TDATA, USAGE=A80, ACTUAL=A80,$

FILEDEF TOFILE DISK test.dat

DEFINE FILE TOFILE
FIELE1/A1 = EDIT(TODATA, '9');
FIELD2/A3 = EDIT(TODATA, '$999');
FIELD3/A10 = EDIT(TODATA, '$$$$9999999999');
END
TABLE FILE TOFILE
PRINT FIELD2 FIELD3
IF FIELD1 EQ '1'
ON TABLE HOLD AS MYFILE
END

Change your define for field1 eq '2' and pull out those records into another file.


2nd method.

This method uses dialog manager to create two fixed format files from one, just created an appropriate mfd to read them once they're created:

FILEDEF TOFILE DISK test.dat
FILEDEF MYFILE1 DISK test1.dat
FILEDEF MYFILE2 DISK test2.dat

-SET &SPC = ' ';
-LOOP1
-READ TOFILE &CHK.A1. &XRECORD.A16.
-IF &IORETURN GT 0 THEN GOTO IMDONE;
-*
-IF &CHK EQ '2' THEN GOTO F2TYP;

-*-----------------
-* PROCESS FILETYPE '1' RECORDS
-*-----------------

-SET &F1FIELD = EDIT(&XRECORD, '999');
-SET &F2FIELD = EDIT(&XRECORD, '$$$9999999999');
-SET &WLINE = &F1FIELD | &SPC | &F2FIELD;

-WRITE MYFILE1 &WLINE

-GOTO LOOP1
-*
-F2TYP
-*-----------------
-* PROCESS FILETYPE '2' RECORDS
-*-----------------

-SET &F1FIELD = EDIT(&XRECORD, '9999');
-SET &F2FIELD = EDIT(&XRECORD, '$$$$999999999999');
-SET &WLINE = &F1FIELD | &SPC | &F2FIELD;

-WRITE MYFILE2 &WLINE

-GOTO LOOP1
-*

-----------------
-* ALL FINISHED
-*---------------
-IMDONE


Hope this helps.....................

 
There actually is a THIRD method. You can describe a file containing records with differing layouts usin a facility in the master file description called RECTYPE. In a nutshell, you describe each different record type as a different segment. Within EACH segment, at the same position within the record, describe a field called RECTYPE, at the location within the record which identifies that record. The ALIAS for the field is the value associated with that RECTYPE. In your case, you say the third and fouth characters determine the record layout, so, within each segment, you'd have a field called RECTYPE, identifying the third and fourth character in that segment. This is covered in great detail in the WebFOCUS Describing Data manual, starting on page 5-21.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top