×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Reading from a text file

Reading from a text file

Reading from a text file

(OP)
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?

RE: Reading from a text file

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.....................

 

RE: Reading from a text file

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close