PDA

View Full Version : EOF problem



chris_uk_lad
08-05-2008, 11:54 AM
Hi.

Im running this code atm which reads in a text file and outputs it in a different format in another txt file. The line altered depends upon the first 2 characters in that line (either DB DJ or T).


Option Compare Database
Option Explicit

Public Function Textfile()

Dim strLine As String
Dim manLine As String
Dim start As String
Dim smallStart As String
Open "C:\Input.txt" For Input As #1
Open "C:\Output.txt" For Output As #2
Line Input #1, strLine
Do While Not EOF(1)

start = Left(strLine, 2)
smallStart = Left(strLine, 1)

If start = "DB" Then
manLine = Left(strLine, 90) & Mid(strLine, 91, 20) & Mid(strLine, 131)
Print #2, manLine
Else
If start = "DJ" Then
manLine = Left(strLine, 22) & Mid(strLine, 23, 28) & Mid(strLine, 73, 28) & Mid(strLine, 123, 28) & Mid(strLine, 173, 28) & Mid(strLine, 223)
Print #2, manLine
Else
If smallStart = "T" Then
manLine = Left(strLine, 76) & Mid(strLine, 115, 2)
Print #2, manLine
Else
Print #2, strLine
End If
End If
End If

Line Input #1, strLine
Loop
Print #2, strLine
Close #1
Close #2
End Function

The problem that i have is that once it reaches the end of the file (last line is the only occurance of a T starting line) it doesnt read the T and just ends the function. Not sure if this is because its a single character instead of a double, or because its the last file in the txt doc.

All help appreciated.

CreganTur
08-05-2008, 12:05 PM
It's not an EOF problem... it's because you're If...ElseIf statements are incorrectly formatted.

Try this:
Public Function Textfile()

Dim strLine As String
Dim manLine As String
Dim start As String
Dim smallStart As String
Open "C:\Input.txt" For Input As #1
Open "C:\Output.txt" For Output As #2
Line Input #1, strLine
Do While Not EOF(1)
start = Left(strLine, 2)
smallStart = Left(strLine, 1)
If start = "DB" Then
manLine = Left(strLine, 90) & Mid(strLine, 91, 20) & Mid(strLine, 131)
Print #2, manLine
ElseIf start = "DJ" Then
manLine = Left(strLine, 22) & Mid(strLine, 23, 28) & Mid(strLine, 73, 28) _
& Mid(strLine, 123, 28) & Mid(strLine, 173, 28) & Mid(strLine, 223)
Print #2, manLine
ElseIf smallStart = "T" Then
manLine = Left(strLine, 76) & Mid(strLine, 115, 2)
Print #2, manLine
Else
Print #2, strLine
End If
Line Input #1, strLine
Loop
Print #2, strLine
Close #1
Close #2
End Function


Both BOF and EOF are areas that exist before the first record, and after the last record. The last line of a text file is actually before EOF.

chris_uk_lad
08-06-2008, 12:39 AM
Afraid it still doesnt work, the 'start' variable never even reads in T_ to even check it against, DJ being the last picked up amount in the sample:

H208
DA02
DB02
DJ08
T004

CreganTur
08-06-2008, 05:13 AM
I created an Input.txt and Output.txt file in my C:\ drive, and put your sample into the Input.txt file.

I ran the exact code that I posted above, and it copied your examples into Output.txt. I ran it a second time by stepping through the procedure, and it worked- evaluated the 'T' line and everything.

I cannot see a problem, becuase it wrote the data into Output.txt- all of it including the last line: T004. The issue I can see from a testing standpoint is that your script is meant to deal with extremely long text strings, but the examples you provided are not long enough to test the full functionality of the code.

Are you working with a regular .txt file, or are you working with a fixed-width text file, or some other formatting variant?