• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 736
  • Last Modified:

help with fileio in vb6


i want to read a file in in vb. i'm a little new to the syntaxt so hope you can help.

basically i want to open and read a file one line at a time. Each line that is read in is structure i.e. chars 0-15 are description 16-20 user code etc. How can i do this?

I have created an additional module with the following code however it doesn't seem to be findint he file. Am i doing something wrong?

Public Function OpenFile(f As String) As Boolean

Set fs = CreateObject("Scripting.FileSystemObject")

If fs.FileExists(getFilename()) Then
    Open getFilename() For Random As #1 Len = 200
    fopen = True
    OpenFile = True
End If

fopen = False
OpenFile = False

End Function

Public Function ReadLine() As String

If fopen Then
    Line Input #1, sBuffer
    ReadLine = sBuffer
End If

ReadLine = Null

End Function

where i read the value for the file in from the command line. (this is working as the string is correct that is being passed ( c:\test.txt ) )

any ideas?
  • 3
  • 2
1 Solution
If the 'lines' are delimited [and a CR/LF sequence at the end], then you can use

    Dim fso     As New FileSystemObject
    Dim fle     As File
    Dim txs     As TextStream
    Dim sLine   As String
    Set fle = fso.GetFile("youFile")
    Set txs = fle.OpenAsTextStream(ForReading)
    Do While Not txs.AtEndOfStream
        sLine = txs.ReadLine
        Debug.Print Left$(sLine, 15)
        Debug.Print Mid$(sLine, 16, 4)
flynnyAuthor Commented:
hi i've tried this and i still get a runtime error 424 object requied when trying to GetFile(<my String>)

the text i have read in is D:\TEST.tRN and this is file is present in the D:\ root and the string does read this. is there something i'm doing wrong here??

Many Thanks,

What's the actual structure of the file - if it's text, can you post up a few lines of it.  If it's binary email it.
flynnyAuthor Commented:
hi i've found the prob there a trailing space before the string due to how i read do you know if there is any trim methods i can use to remove this.

i have " D:\test.tRN"
fileName = Trim$(" D:\test.tRN")
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now