PDA

View Full Version : Inputting one text file, and writing out another... Can this be done?



gregorywest
22-01-2015, 23:21
I have hit an issue that should be very simple to deal with.

I have one text file from the IBM AS/400 with invoice information. Not I could download the information, but that is a lot of work, not the least of whiich is converting EBCDIC to ASCII and getting all the AS/400 ports and security correct. Instead, since I print through a windows computer it is easier to print the invoices to a TXT file.

Now all I have to do is write a quick down and dirty basic program to read in the data massage it and write out the import file I need.
My problem is getting the ThinBASIC syntax to open the two files up. I do have 100% control over the file names, "S:\Invoice_Export.exe" and "S:\Invoice_Import.txt" The Export file would be opened read only, and written out to the Import file overwriting any import file that might be there already.

Basically the program would look like this:

Open exportfile readonly
Open importfile overwrite
write importfile "Header information and formatting"
Loop: gosub process_invoice
write importfile invoice_string 'created by process invoice
if not eof(exportfile) goto Loop
close exportfile
close importfile
end

function process_invoice
this is a little complex
basically a bunch of reads from exportfile and logic to figure out what was read in.
endfn


The file itself would look something like:
S983663523

8765432

2015-01-15 23432 432423 4324232 4324224

01 8373764 A DESCRIPTION LINE 2 123.00 246.00
MORE DESCRIPTION

02 948873 AND ON WE GO FOR MULTIPLE LINES 1 123.00 123.00

I can write the code to deal with what is in the file, just looking for the syntax of how to open, read, write to the TXT files.


Thanks in advance
Greg

gregorywest
22-01-2015, 23:36
I started looking at the example code. This one of the examples is exactly what I need. Will just have to massage it a bit and will work wonders for me. Great program guys!!!!

ReneMiner
23-01-2015, 10:32
you found it already?


There are two ways, either use the FILE-module and have an arsenal of file-functions available - or you might just use core-functions Load_File/Save_File to read/write the file in one go- they are equivalent to File_Load & File_Save.



String sData = Load_File(sFilename)
'...
Save_File(sFilename, sData)


The data you obtain then is a common dynamic string and you might use string-functions of core-module to modify/parse data. Enhanced you can use "Tokenizer"-module for fast splitting data into separate tokens - but then you might have to re-assemble the string before saving data.

If you have numerical/fixed size data that follows a certain pattern, thinBasic allows you to use a very simple way to interpret this data as udt.
Just place some udt dimensioned to the count of records in your data upon your data.

example:


' setup some udt that matches the data-pattern but it must not contain dynamic strings!

Type t_myUDT
A As Long
B As Byte
C As Double
D As String * 32 ' fixed size strings are allowed
'...
End Type

' get data from file into a string
String sData = Load_File("C:\myFile.dat")

' obtain number of data-elements/records
Long nRecords = Len(sData) / SizeOf(t_myUDT)

' place the udt upon the string to have an array of single records now:
Dim record(nRecords) As t_myUDT At StrPtr(sData)


' now can access each single record() of sData.

' you could add some blank record at the end like this:
sData &= Repeat$( SizeOf(t_myUDT), MKBYT$(0) ) ' append 0-bytes

' be aware:
' if you change the size of string sData you also have to
' Redim record( Len(sData)/SizeOf(t_myUDT) ) At StrPtr(sData)


If your data altogether is very large then you might consider to use "StrPtrLen(StrPtr(sData))" instead of "Len(sData)" - same result but Len() will create a local string thats allocation consumes time and memory while requesting the StrPtrLen of a StrPtr will only peek out a dword in front of the stringpointer.

gregorywest
23-01-2015, 18:19
That looks good too. Not sure would work for the situation I am in right now, but for the future, yup that would be great.

The input file I am working with now is between 2K and 700M in size. Could be either extreme, and it can be processes top to bottom, no need to load the entire file at once. But I do like your solution!!

Greg

ErosOlmi
23-01-2015, 19:01
Hi Greg,

thanks a lot to be here with us and for your donation.

regarding reading /writing text files, you can see some examples going into \thinBasic\SampleScripts\File\ directory.
There are some different ways you can read text files, each has some pro/cons depending on what you need to to.

\thinBasic\SampleScripts\File\File_ReadAndPrintLine.tbasic can be a start.
It uses easy to use File_LineInput and File_LinePrint functions. The cons is that reading line by line can be slow on big files due to end of line parsing.

If the input file is fixed length line, binary reading a fixed number of bytes is much more efficient.

Regarding GOTO ...
ThinBasic has no GOTO keyword. The main reason is that having GOTO would produce quite unreadable code.
ThinBasic has a lot of different execution control flow that can be used to place for GOTO.

Anyway, we are here to help if we can so if any doubt ... post here and we will reply.

Ciao
Eros