[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 713
  • Last Modified:

help with fileio in vb6

Hi,

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?
0
flynny
Asked:
flynny
  • 3
  • 2
1 Solution
 
peetmCommented:
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)
   
    Loop
0
 
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,

Matt.
0
 
peetmCommented:
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.
0
 
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.e.
i have " D:\test.tRN"
0
 
peetmCommented:
fileName = Trim$(" D:\test.tRN")
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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