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!

Another text file question? 1

Status
Not open for further replies.

Terence

Technical User
Feb 22, 2001
30
US
Hi All, i am using this code to achieve the results i need to read from one file and then save it to another. This works for me until i save it to the file. What happens is the code works but every once and while instead of reading just one line it will read two lines, it will process the first line and then put the other line right after the first line without porcessing it.
i think the problem is that the file i am reading is to big (70 pages of text) but i am not having any luck as to getting anything else to work.

HElp required.......


Open getfile For Input As #1


Open "c:\Atextout.txt" For Output Shared As #2




Do Until EOF(1)


Line Input #1, InputString

OutputLine = Mid(InputString, 3, 12)

OutputLine = OutputLine & Space(3)

OutputLine1 = OutputLine & Mid(InputString, 14, 10)

OutputLine2 = OutputLine1 & Space(3)


OutputLine3 = OutputLine2 & Mid(InputString, 23)


Print #2, OutputLine3, Tab(1)

Loop


Close #2
Close #1




 
You must always take errors into account during read/write operations. A better way is use the LOF function to determine the length of a file and read all characters in one operation with the Input$ function. Thereafter, use the Split string function to place the contents into an array and then process each element in the array as needed.

Code example:

Sub File_RW()
Dim strData as String
Dim StrLineItems() as String, i As Long
Dim Lower as Long, Upper as Long
Dim strOut As String
Dim fnum1, fnum2

fnum1 = FreeFile()

Open GetFile For Input As fnum
strData = Input(LOF(fnum), fnum)
strLineItems() = Split(strGetFile, vbCrLf)
Lower = LBound(strLineItems)
Upper = UBound(strLineItems)

fnum2 = FreeFile()

Open "C:\ATEXTOUT.TXT" For Output Shared As fnum2

For i = Lower To Upper

strOut = Mid(strLineItems(i), 3,2) _
& Space(3) _
& Mid(strLineItems, 14, 10) _
& Space(3) _
& Mid(strLineItems, 23)
Print #2, strOut, Tab(1)

Next i

Close fnum1
Close fnum2

End Sub
 
Thanks alot StewardGW with a little tweaking it works fine. One other question if you don't mind, i started to look at the "File system object " as maybe a better way of doing this, is this a better way and can you us the same MID to break up the strings?

Thanks again....... s-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top