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

Moving around in open text file

Status
Not open for further replies.

EddyLLC

Technical User
Mar 15, 2005
304
US
The following code opens up a file and after confirming the "MR" opens up a second file and moves data in 80 character sections from one to the other. The problem is the file is one long string of data without carriage returns thus I am at the end of the file after the Line Input statement. My question, How do I go back to the beginning of the record or file so I can load the sections. Do I have to close the file then open it up again? Thanks

Do While (Len(strFile) > 0)
Open strFolderName & "\" & strFile For Binary As #1
Line Input #1, str1
strAppId = Mid(str1, 91, 2)

If strAppId = "MR" Then
Open Results For Output As #2
Dim MyRecord As String
'Sets variable MyRecord to first 80 characters of string.
MyRecord = String(80, " ")
'
Do While Not EOF(1)
Get #1, , MyRecord 'Reads first 80 charcters.
Print #2, MyRecord 'Prints MyRecord(first 80 characters) into file Results #2
Loop
Close #1
Close #2
End If
strFile = Dir
Loop
 
Eddy,
I am not real good on random access files, but since you have everything in one readln could you not just save this to a variable and then parse it out. This code does that.

Code:
Public Const intchunksize As Integer = 3


Public Sub write80()
  Dim myStr As String
  Dim outStr As String
  myStr = "acdefghijklmnopqrstubwxyz"
  Do Until Len(myStr) = 0
    If Len(myStr) > intchunksize Then
      outStr = Left(myStr, intchunksize)
      Debug.Print outStr
      myStr = Mid(myStr, intchunksize)
    ElseIf Len(myStr) = intchunksize Then
      outStr = Left(myStr, intchunksize)
      Debug.Print outStr
      Exit Sub
    Else
      outStr = Left(myStr, Len(myStr))
      Debug.Print outStr
      Exit Sub
    End If
  Loop
End Sub

I would think you just change the constant to 80, and change the debug.print to writelns. Here is my output

abc
cde
efg
ghi
ijk
klm
mno
opq
qrs
stu
uvw
wxy
yz

Just a thought.
 
This

Code:
Dim myLine As String        'The read line
Dim iChars2Write As Long    'Charachters to write out
Dim lCount As Long          'A Counter
Dim myWrite As String       'The 80 chars line written out

For lCount = 1 To Len(myLine) Step iChars2Write
    myWrite = Mid(myLine, lCount, iChars2Write)
    Print #2, Left(myWrite & Space(iChars2Write), iChars2Write)
Next lCount

should write iChars2Write line to yor #2 output file
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top