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

file help plz 1

Status
Not open for further replies.

just1and0

IS-IT--Management
Jul 30, 2004
9
US
Hello everybody

Needing some help with a problem. I have a file with a header and details records but file can have multiple header. Here is a sample

0720040050720H32B75W
10161007178541051100010
10161007178541055900010
10161007178541510900010
10161007178541057000010
10161007178541051300010
10161000733690601100010
10161007178541062300010
10161007178541063000010
10171000180006562000010
10171000180006554000010
0720040050720N126708
10440000000046401600200
0720040050720N126708
10011000710070900900040
10011000710070100100020
10011000710070200200010
10011000447000036800010
10011000710074053300010
10011000710074053100010

this is wht i wanna do read the file if header record read the detail till next header. add header record with details something like

100110007100709009000400720040050720N126708
... and so on. Problem gets even better i have 30 file like this that need to be appended in the newly created outfile.

Greatful for any help provided.

if anybody interested here is FD for the file

*FD ORDER-INPUT-FILE
DATA RECORD IS ORDER-INPUT-REC.
01 ORDER-INPUT-REC.
03 ORDER-INPUT-RECORD-TYPE PIC X.
*************RECORD-TYPE 0 = Header 1 = Detail
03 ORDER-INPUT-HEADER.
05 ORDER-INPUT-SOURCE PIC X.
05 ORDER-INPUT-FORMAT PIC X.
05 ORDER-INPUT-APPLIC PIC XX.
****************Store 1=4001, 2=4002, 3=4004, 4=4005
05 ORDER-INPUT-STORE-NUMBER PIC 9(4).
05 ORDER-INPUT-SHIPPING-MMDD PIC 9(4).
05 ORDER-INPUT-SERIAL-NO PIC X(.
05 FILLER PIC XX.
03 ORDER-INPUT-DETAIL REDEFINES
ORDER-INPUT-HEADER.
05 ORDER-INPUT-BREAKER-CODE PIC 999.
05 ORDER-INPUT-TYPE-INDICATOR PIC X.
****************AWG No/UPC Code will be numeric, right justfied
****************The field below can be either AWG no or UPC Code,
****************depending on the Type Indicator
05 ORDER-INPUT-AWG-UPC-NO PIC X(13).
05 ORDER-INPUT-QTY PIC 9(4).
05 ORDER-INPUT-LABEL-INDICATOR PIC X.
 
Sounds simple enough.
The tricky part seems to be recognising that you have read a header record.


Think around this pseudo code
(it won't run as it stands, this is just a brain dump)

szHeader = string
szRecord = string
open the input file
open the output file
check for EOF on the input file

Read a record into szHeader (should be a header record)
If not a header record, give up

do
read a record into szRecord
if not a header
append szHeader to end of szRecord
output to out file
else
szHeader = szRecord
end if
until in.eof
close both files




 
its both header and detail.

first digit 0 or 1 is the check digit. 0---> header
and 1---> detail.

I have posted file defination in cobol for the file. May be that will help a little.Plz let me know if you need any more info.

Thank you very much.
 
Triffic.

So the bit in the middle becomes

if left(szRecord,1) ="1"
output szRecord & szHeader to output file
else
szHeader = szRecord
end if
 
Thank you very much i was also think some thing simillar but didn't know should i read a line or char by char. My i ask for a favor from you guys little code will help make my life little simpler havn't done any coding in 2 years though i know c++ and vb.
 
Are you doing this only once? or do you need a program to do this automated.

If this is a 1 time only deal, you ahve to convert 30 files and will probably never do this again, then it would be faster to inport the Data into Excel and have that program split it for you.

Casper

There is room for all of gods creatures, "Right Beside the Mashed Potatoes".
 
This is goin to be a automated process run almost everyday . this is a subset of a problem.

Thank you
 
Jeff

I got file to read as i wanted but the problem is it only reads first header and its records not the rest is there any way we can fix that.here is my code

Dim TextLine
Dim header As String
Dim detail As String

'TESTFILE = "c:\test.txt"
Open "c:\test.txt" For Input As #1 ' Open file.
Open "c:\testout.txt" For Output As #2 ' Open file.
Do While Not EOF(1) ' Loop until end of file.
Line Input #1, TextLine ' Read line into variable.
If (Left(TextLine, 1) = "1") Then
detail = TextLine
record = detail + header
Print #2, record

Else
'MsgBox ("Header info")
header = TextLine
End If
Loop
'MsgBox ("File Done")
Close #1 ' Close file.
Close #2

thank you
 
That looks fine to me.
Have you tried putting a msgbox or debug.print in just after the

Line Input #1, TextLine ' Read line into variable.

to see what you are getting?
Also put a breakpoint on

header = TextLine

to make sure you are recognising a change to header.

You should DIM record as string
and TextLine as String they are currently variants.

This will lead to VB trying to interpret them as numbers.
If they have leading zeroes, you will lose these on the output stage.

Making then strings will avoid this 'gotcha'


 
Jeff

you are correct. I did put break point it is reading first header and its records but when 2 header is reached getting out of the loop. Plz advise me how can i fix that.

Dim TextLine As String
Dim header As String
Dim Detail As String
Dim record As String

'TESTFILE = "c:\test.txt"
Open "c:\test.txt" For Input As #1 ' Open file.
Open "c:\testout.txt" For Output As #2 ' Open file.
Do While Not EOF(1) ' Loop until end of file.
Line Input #1, TextLine ' Read line into variable.
MsgBox ("Hello TextLine")

If (Left(TextLine, 1) = "1") Then
Detail = TextLine
record = Detail + header
Print #2, record

Else
' MsgBox ("Header info")
header = TextLine
MsgBox ("Hello Header")
'Debug.Print TextLine ' Print to the Immediate window.
End If
Loop
MsgBox ("File Done")
Close #1 ' Close file.
Close #2

by the way i am using access.
 
When it leaves the loop, what is the value of EOF(1)?
It should stay in the loop until it reaches the last row.
What is the value of the second header?
have you in fact reached the end of the file and gotten a blank row? That would 'seem' to be a header by virtue of not beginning with a '1'

Open the specific source file in Notepad and check the contents.
Make sure there are no blank lines, or that (if there are) you code around them by skipping the line.
 
Jeff

It reads the second header but then exit the loop. No blank line i double checked.I am using the same sample file i posted.I tested as you told me . i think as you said it is having dificulity recognising a change to header.Its not even close to EOF. Please advise.
 
Jeff

Well What do you know it worked Stupid machine. Well Jeff i am very thankfull to you. Now the next problem i have 30 file that need to be read same thing and append all of these into one file any ideas?

Thank you once again.
 
No problem.

Put the 30 file names into an array like so:

Dim szFiles(30) as string
szFiles(1) = "C:\bar1.txt"
szFiles(2) = "C:\fred3.txt"
...etc

Wrap the code you are using in a for...next loop

Dim iLoop as integer

For iLoop = 1 to 30

...existing code

Next



Change the lines that currently read
Code:
Open "c:\test.txt" For Input As #1    ' Open file.
Open "c:\testout.txt" For Output As #2    ' Open file.

To be
Code:
Open szFiles(iLoop) For Input As #1    ' Open file.
Open "c:\testout.txt" For APPEND As #2    ' Open file.

and that's about it...



If the files are all held in one folder, you could get elegant and use filesystem objects, or repeated calls to DIR() to find the names of the files and process them one by one instead of having a hard coded list.
I'll leave that as an exercise for you..
 
Jeff
thank you very much sir problem solved i was able to get all the file in one file.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top