CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2000
    Posts
    24

    text file opening

    hello, i made a text file program like notepad where you save and open text files, but when i open a text file with spaces it only opens one line of it, heres the code for opening it, tell me whats wrong:

    Private Sub mnuopen_Click()
    On Error GoTo h
    Dim strTemp As String
    dlg.Filter = "Text files (*.txt)|*.txt"

    dlg.FileName = "*.txt"
    dlg.ShowOpen
    If Dir(dlg.FileName) > "" Then

    Open dlg.FileName For Input As #1

    Line Input #1, a
    text1 = a

    Close #1
    Else
    MsgBox "File not found", vbExclamation, "Error" & Err.Description
    End If
    h:

    End Sub



  2. #2
    Join Date
    Jan 2000
    Location
    Saskatchewan, Canada
    Posts
    595

    Re: text file opening

    Well, It looks as if you only read one line. You would have to add the following code to read the rest.

    Open dlg.FileName for input as #1

    while not eof(1)
    Line input #1, a
    text1 = text1 & a
    wend

    Close #1





    David Paulson

  3. #3
    Join Date
    Jan 2000
    Location
    Saskatchewan, Canada
    Posts
    595

    Re: text file opening

    You may also have to add a cr/lf
    text1 = text1 & vbcrlf

    David Paulson

  4. #4
    Join Date
    Jan 2000
    Location
    MO, USA
    Posts
    1,506

    Re: text file opening

    Your code only reads one line of the source file - you could do what the above post suggests. I personally have become very partial to the object way of using text files - the FileSystemObject:

    'set a reference to the Microsoft Scripting Runtime

    private Sub mnuopen_Click()
    Dim FSO as Scripting.FileSystemObject
    Dim Filer as Scripting.TextStream
    Dim sFile as string
    Dim strTemp as string

    on error GoTo h
    dlg.Filter = "Text files (*.txt)|*.txt"

    dlg.FileName = "*.txt"
    dlg.ShowOpen

    If Dir(dlg.FileName) > "" then
    sFile = dlg.FileName
    set FSO = new Scripting.FileSystemObject
    set Filer = FSO.OpenTextFile(sFile, ForWriting, false)

    text1.Text = filer.readall

    Filer.Close
    else
    MsgBox "File not found."
    End If

    set Filer = nothing
    set FSO = nothing
    End Sub




    Hope that helps,

    John

    John Pirkey
    MCSD
    http://www.ShallowWaterSystems.com
    http://www.stlvbug.org
    John Pirkey
    MCSD (VB6)
    http://www.stlvbug.org

  5. #5
    Guest

    Re: text file opening

    Add a loop to scan from the begin to the end of file :

    open ....

    while not eof (1)
    line input #1, a$
    text1 = text1.text & a$
    loop

    close 1




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