CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2001
    Location
    New York, USA
    Posts
    169

    Reading/Parsing text files

    I can't figure out why this code doesn't display the whole text file. There are 86 numbers in that text file. For example,

    11111
    22222
    33333
    44444
    55555
    .
    .
    .

    Only the first 13 numbers are displayed in the textbox, but the rest are just blanks, Chr(13), and Chr(10).

    Does any of you know why it's doing that?

    THANKS!!!!


    Dim FileNum as string, strText as string, tmpPath as string
    Dim i as Integer, EndPos as Integer, tmp as Integer

    tmpPath = "C:\My Documents\Test.txt"
    FileNum = FreeFile

    Open tmpPath for input as FileNum
    strText = input$(LOF(FileNum), #FileNum)
    Close #FileNum

    '### Chr(10) and Chr(13) are seperators ###
    strText = Replace(strText, Chr(10), "")
    i = InStr(1, strText, Chr(13))

    EndPos = 1
    tmp = i
    '### Display every number ###
    Do While i > 0
    me.Text1.Text = Text1.Text & Chr(10) & mid(strText, EndPos, tmp)
    EndPos = EndPos + i
    i = InStr(i + 1, strText, Chr(13))
    Loop





    [email protected]

  2. #2
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868

    Re: Reading/Parsing text files

    Not sure why your's isn't working but set a reference to Microsoft Scripting Runtime (scrrun.dll) and try this code:


    option Explicit

    private Sub Command1_Click()
    Dim fs as FileSystemObject
    Dim ts as TextStream

    set fs = new FileSystemObject
    set ts = fs.OpenTextFile("C:\My Documents\Test.txt", ForReading)

    Text1.Text = ""
    Do While Not ts.AtEndOfStream
    Text1.Text = Text1.Text + ts.ReadLine + vbCrLf
    Loop
    ts.Close
    set ts = nothing
    set fs = nothing

    End Sub






  3. #3
    Join Date
    Aug 2001
    Location
    New York, USA
    Posts
    169

    Re: Reading/Parsing text files


  4. #4
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: Reading/Parsing text files

    Not quite sure why you ar going to such pains to read a text file into a textbox. Here is a much simpler method

    ' Read a file into a TEXT box using binary read
    ' this reduces the exposure of VB editing double quote marks and
    ' commas out of the text

    private Sub Command1_Click()
    Dim a
    a = "C:\to DO LIST\1.txt"
    Open a for binary as #1
    Text1.Text = input(FileLen(a), #1)
    Close

    End Sub




    John G

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured