Send a suggestion!

We're building a brand new version of the site, and we'd love to hear your ideas

Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 73,375 times

Contents

Related Categories

Files and Folders - Reading files line by line

Reading files line by line

It is often useful to read a file line by line, especially if you need to parse the file if you have created your own special file format. Please note that this method is slower than reading the file all at once, so only use it if you really need to load the file line by line!

Dim nFileNum As Integer, sText As String, sNextLine As String, lLineCount As Long

' Get a free file number
nFileNum = FreeFile

' Open Test.txt for input. App.Path returns the path your app is saved in
Open App.Path & "Test.txt" For Input As nFileNum
lLineCount = 1
' Read the contents of the file
Do While Not EOF(nFileNum)
    Line Input #nFileNum, sNextLine
    'do something with it
    'add line numbers to it, in this case!
    sNextLine = lLineCount & " " & sNextLine & vbCrLf
    sText = sText & sNextLine
    lLineCount = lLineCount + 1
Loop
TextBox1.Text = sText

' Close the file
Close nFileNum

Note that the Line Input function returns the current line to sNextLine. This does not include the new line character, so if you are displaying the text you need to add one!

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • help

    Posted by tieungao35 on 09 Nov 2005

    Hi all,
    I am a new beginner of Visual basic 6. Even though i read James's article, i don't know how to create a project and add his code as a new module.
    Could you please show me how to create 1...

  • INI files

    Posted by TomDooley on 26 Sep 2005

    Hello,

    the article of James is not state of the art.
    To import win32 functions is not the way how you should work with the .NET framework. With a little bit pation you can write a own managed code...

  • Copying folders including subdirectories

    Posted by kingman29 on 07 Jul 2005

    I am using deppfreeze in my internet cafe.This program protects hard drive from every changes and does not allow save games.When computer restarted all saves dissapear from hard drive.I know the game...

  • ei.

    Posted by po_tph on 20 Mar 2005

    do you a source code of hing folders???

  • hide folder source code???

    Posted by po_tph on 20 Mar 2005

    can anybody plz help me i want to have a sourcec code of hidng folders in windows directory.thanks