How can I read data from a text file using VB6?

前端 未结 7 1638
悲哀的现实
悲哀的现实 2020-12-06 07:22

I need to read data from text files and use the same in my application. Im using VB 6.0. What commands do I use? Some Sample code would be highly appreciated.

相关标签:
7条回答
  • 2020-12-06 07:46

    if there is just plain text in the file then you can read in the whole into 1 string variable with the following code :

    Private Sub ReadFile(strFile As String)
      Dim intFile As Integer
      Dim strData As String
      intFile = FreeFile
      Open strFile For Input As #intFile
        strData = Input(LOF(intFile), #intFile)
      Close #intFile
    End Sub
    

    a variable-length string can contain up to approximately 2 billion (2^31) characters

    0 讨论(0)
  • 2020-12-06 07:49

    A full tutorial and sample code can be found here

      Open Filename$ For Input As #FileHandle
    
      Do While Not EOF(FileHandle)        ' Loop until end of file
       Line Input #FileHandle, TextLine$  ' Read line into variable
        ' Your code here
      Loop
    
      Close #FileHandle
    
    0 讨论(0)
  • 2020-12-06 07:49

    Here is the code for that

    Function ReadFileToText(filePath)
    
       Dim objFile, objText, text
    
       Set objFile = CreateObject("Scripting.FileSystemObject")
       Set objText = objFile.OpenTextFile(filePath)
    
       text = objText.ReadAll
       objText.Close
    
       Set objText = Nothing
       Set objFile = Nothing
    
       ReadFileToText = text
    
    End Function
    

    More details you can check http://smartreferences.blogspot.in

    0 讨论(0)
  • 2020-12-06 08:00

    i will refer you a different method to read and import the content to your form window

    public sub readfile
        Dim rtc As TextBox = New TextBox
        rtc.Multiline = True
        rtc.ScrollBars = ScrollBars.Both
        rtc.Width = 400
        rtc.Height = 200
        Me.Controls.Add(rtc)
        rtc.WordWrap = True
        Dim FILE_NAME As String = "C:\Users\vcidex92\Desktop\suji\me.html"
    
        If System.IO.File.Exists(FILE_NAME) = True Then
    
            Dim objReader As New System.IO.StreamReader(FILE_NAME)
            rtc.Text = objReader.ReadToEnd
            objReader.Close()
        Else
    
            MsgBox("File Does Not Exist")
        End If
    end sub
    
    0 讨论(0)
  • 2020-12-06 08:03

    Make sure your file exists:

    If myFSO.FileExists(IN_sFilePath) Then
        Set myTextStream = myFSO.OpenTextFile(IN_sFilePath, ForReading)
        myString = myTextStream.ReadAll()
        Call myTextStream.Close
    End If
    'Assign Return Value
    ReadTextFileAsString = myString
    
    0 讨论(0)
  • 2020-12-06 08:05

    I'm a little late to the game here, but the FileSystemObject that is part of the Microsoft Scripting Runtime (scrrun.dll) can be pretty useful for this.

    Public Function ReadTextFileAsString(IN_sFilePath As String) As String
        Dim myFSO As Scripting.FileSystemObject
        Dim myTextStream As Scripting.TextStream
        Dim myString As String
    
        'Create a new FileSystemObject
        Set myFSO = New Scripting.FileSystemObject
    
        'Make sure file exists:
        If myFSO.FileExists(IN_sFilePath) Then
            Set myTextStream = myFSO.OpenTextFile(IN_sFilePath, ForReading)
            myString = myTextStream.ReadAll()
            Call myTextStream.Close
        End If
        'Assign Return Value
        ReadTextFileAsString = myString
    
        'Make sure to clean up when done.
        Set myTextStream = Nothing
        Set myFSO = Nothing
    End Function
    

    There are a number of other methods available for getting data from the text stream. You can also read a certain number of characters at a time, or line-by-line. You will need to add the Microsoft Scripting Runtime into your project references, but it is really very useful.

    0 讨论(0)
提交回复
热议问题