Click to See Complete Forum and Search --> : Can you create a client server system using flat files


coffeyr
September 16th, 1999, 07:31 AM
I am trying to write a program that will read records from a flat file (around 40-50 characters on a line, then there is a carriage return line feed and then next record).

Then I would like to convert/extract pertinent info from each record to send it to an AS/400 server, preferably with a time stamp, a 12 character number and 0 or 1 key number for identification purposes.

Finally, test to see if the flat file is at the end of file (obviously, if false, continue with next record). If true, Write to a new file (a backup file that will contain the data from the original flat file) and delete the original flat file.

An example of the flat file, as follows:

199909020646500700201610016782000000000NL070V100000000I4X1FA15B1C10D6V1000025054054100026001002
199909020646520700201600240195000000000NL070V1000000000I4X1FA15B1C9D6V1000025054054200026001002
199909020646540700201600284792000000000NL070V1000000000I3X1FA15B1C8D6V1000025054054300026001002

What I know!

I will open the file using Open and then use the Line Input statement to read the data one record at a time. Then use the Eof function to see when I have reached the end of the file.
I don't know how to send the information to the other computer. The middleware and communication between the PC and server is MQSeries (The AS/400 will not return anything to the PC).
I do not know how to send the information and how to write to a new file and continually append to it (without deleting anything from the written file)....

Thank you for your help in advance!
Respectfully,

Robert Coffey

Chris Eastwood
September 16th, 1999, 08:26 AM
I take it that each line from the flat-file relates in some way to a data structure from a MainFrame / Other computer system ?

If this is the case, then I've recently written something that handles the reading of the flat file in an easy manner :

1. Declare a 'type' that matches the one on the other system, eg.

On our system we had 'Type G' records that matched this declaration :


public Type GRecord
RECTY as string * 1 ' Record Type
Tag as string * 9 ' An Internal Key
FILL01 as string * 16 ' A FileName
SHORT as string * 40 ' A Short Description
LONG as string * 80 ' A Long Description
FILLER as string * 114 ' A 'padding' record
End Type




2. declare the CopyMemory API call - this allows you to copy chunks of memory around - ideal for copying a string directly into a 'type' record
as defined above : (public/private declaration as applicable)


public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(Destination as Any, Source as Any, byval Length as Long)




This allows you to copy a string directly into your UDT (as long as the string is the correct length! Expect lot's of errors otherwise! - See step 3 for example code)


3. Read in a line from the input file, eg :



Dim recG as GRecord
Dim miInputFile as Integer
Dim sTmp as string

miInputFile = FreeFile()

Open "testfile.dat" for input as miInputFile

Line input #miInputFile, sTmp ' stmp holds the 'line'
'
lLen = len(sTmp)
'
' Here's the clever bit - if the string read in from the file has
' been truncated and doesn't match the correct size, pad the string
' with spaces to the required length
'
If lLen < len(recG) then
sTmp = sTmp & string$(len(recG) - lLen, " ")
End If
'
' Now copy the string read from the file directly into the
' recG record structure using the CopyMemory API - this is
' about 10x faster than splitting the string up and
' assigning each property
'
CopyMemory recG, byval sTmp, len(recG)
'
' Now Do something with the recG record
'
' DoSomething recG
'
' Now loop till the end of the file and process each record the
' same way
'
Do

sTmp = ""
Line input #miInputFile, sTmp
If EOF(#miInputFile) then Exit Do

lLen = len(sTmp)
If lLen < mlLenG then
sTmp = sTmp & string$(mlLenG - lLen, " ")
End If

CopyMemory RecG, byval sTmp, mlLenG
' Now Do something with the recG record
'
' DoSomething recG

Loop
'





>I do not know how to send the information and how to write to a new file
>and continually append to it (without deleting anything from the written
>file)....

Sending the Information all depends on your design of the system - you could import it to the file, use a mesaging protocol - it's all up to you.

As for creating the output file: in the above code example, the 'DoSomething' routine could continually write to the output file as such :


private Sub DoSomething(byval RecordG as GRecord)

Dim iFile as Integer

iFile = FreeFile()

Open "outputfile.dat" for Append as iFile

print #iFile, RecordG.TAG
print #iFile, RecordG.LONG
'
' Etc
'
Close #iFile
End Sub




That should help you get started.

Chris Eastwood

CodeGuru - the website for developers
http://codeguru.developer.com/vb