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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading Sequential text files

Status
Not open for further replies.

AlanArons

Programmer
Aug 1, 2002
91
US
I have a very large sequential ASCII SDF text file that includes different record types, (file header, file trailer, batch header, batch detail, etc). Each record type is identified in the same position in the record, but most of the other fields are different depending on the record type. The maximum record length is about 350 characters.

Is there a way I can read each record in sequence, determine the record type and then process the record separately. My specific need is to extract a reference number that exists in the batch header record but not in the batch detail record and then include it with the detail record.

Your thoughts will be helpful.

Alan Arons [ponder]
 
It should be pretty easy. You need to read the file line by line, check for an 'anchor' that you can use in an if...endif or case...endcase code block.

Take a look at the character functions in the help and if you need more help post a few sample lines.

Brian
 
I agree with Brian. This situation is not too different than having to do a text parse for EDI files (where each 'record' type may be different) or reading XML the 'hard-way' by doing similar parsing.

If you do a low-level FOPEN and work your way through the lines of text you will be able to identify each 'record' type by knowing that Each record type is identified in the same position in the record and reading that text.

Low-level text parsing is sometimes tedious to develop, but it works well for these types of situations.

Good Luck,
JRB-Bldr
 
I have never used the FOPEN function. Can you give me an example of how it is used

Thanks

Alan Arons [ponder]
 
Code:
READ_FILE("C:\TEMP\MYFILE.TXT")


FUNCTION READ_FILE(m.FILENAME)
	PRIVATE m.FILENAME, m.FILEHANDLE, m.FLG, m.STRING
	M.FLG = .F.
	IF MYFILE(m.FILENAME)
		M.FILEHANDLE = FOPEN(m.FILENAME,0)
		IF m.FILEHANDLE < 0  && Check for error opening file
			= MESSAGEBOX("Unable to create or read file",0,"Error")
		ELSE  
			** use fgets to read a line (up to 255 characters in this case)
			M.DATALOCATION =FGETS(m.FILEHANDLE, 255)
			** if you want a .t. then do a conversion
			M.SHOWSTATUS	=IIF(FGETS(m.FILEHANDLE, 255)=".F.",.F.,.T.)
			M.FLG = .T.
		ENDIF
		=FCLOSE(m.FILEHANDLE)  && Close file
	ENDIF
	RETURN(m.FLG)

** I use MyFile rather than File() because it gives better control of paths
FUNCTION MYFILE
	PARAMETER m.FILENAME
	PRIVATE m.FILENAME,m.FLG
	M.FLG = .F.
	IF !EMPTY(m.FILENAME)
		IF ADIR(TMPDIRFILES,m.FILENAME) > 0
			M.FLG = .T.
		ENDIF
	ENDIF
	RETURN(m.FLG)

Regards

Griff
Keep [Smile]ing
 
Transform a Partially Formatted Delimited Text File Into a Standard Delimited File faq184-4275 has some other examples.
 
Grif,

Your example helps alot.

** use fgets to read a line (up to 255 characters in this case)

Is the 255 characters a physical limit or just the number you chose for the example?

Alan Arons

 
I just chose it, fgets() returns 254 bytes if you leave it out altogether, you can make it as big as about 8K I think - so your 350 characters will be fine.

Fgets() ignores linefeeds and stops reading at chr(13)

Good luck

Regards

Griff
Keep [Smile]ing
 
Griff

Thanks, you have been a great help

Alan Arons [ponder]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top