Results 1 to 6 of 6

Thread: Opening a txt file

  1. #1

    Thread Starter
    Lively Member flamer2010's Avatar
    Join Date
    Apr 2002
    Posts
    96

    Opening a txt file

    How do I put text from a notepad .txt file into a text box on my form?

    I am using this code

    VB Code:
    1. Dim iFileNum
    2. Dim txtInput
    3.  
    4. iFileNum = FreeFile
    5. Open "C:\Test.txt" For Input As iFileNum
    6. Print #iFileNum, txtInput
    7. Close iFileNum
    8.  
    9. Text1.Text = txtInput

    but it says that its a bad file mode. how do I fix this?

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    You can't use Print when the file is open for Input. Try opening it for Output or Append.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    Try this:

    open "file" for binary as #1
    filecontents = input(lof(1),#1)
    close #1
    text1.text = filecontents

  4. #4
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246
    Put this in a module


    Function LoadFile(FileName As String)
    Dim FileNumber As Long
    DIm Data As String

    FileNumber = FreeFile
    If FileName = "" Then Exit Function
    Open FileName For Binary Access Read As FileNumber
    Data = String(FileLen(FileName), Chr(0))
    Get FileNumber,,Data
    Close FileNumber
    LoadFile = Data
    End Function


    'Example Text1 = LoadFile("C:\test.txt")


    Have Fun

    «°°phReAk°°»

    Visual Studio 6, Visual Studio.NET 2005, MASM

  5. #5

    Thread Starter
    Lively Member flamer2010's Avatar
    Join Date
    Apr 2002
    Posts
    96
    may I see some working code plzzz?

  6. #6
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    VB Code:
    1. Dim iFileNum As Integer
    2. Dim txtInput As String
    3.  
    4. iFileNum = FreeFile
    5. Open "C:\Test.txt" For Input As #iFileNum
    6.     txtInput = Input$(LOF(iFileNum),iFileNum)
    7. Close #iFileNum
    8.  
    9. Text1.Text = txtInput

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

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