Visual Studio Visual Basic Read File Line by Line

  1. #1

    coder4life is offline

    Thread Starter

    Lively Fellow member


    read from text file, line past line

    how can i read from a text file i line at a time?

    let's say the file is this

    12345 house
    3245 mouse
    467 open door

    what i want is to keep the text file open up, read the get-go line, do something with it, and then become to the side by side line, do something with it, etc, until i take procesed all the lines in the text file.

    thanks for any ideas


  2. #ii

    Re: read from text file, line by line

    Code:

    Private Sub ReadLines() Dim MyLine As String Open "C:\MyFile.txt" For Input As #ane     Exercise While Not EOF(ane)         Line Input #ane, MyLine         MsgBox MyLine     Loop Shut #1 Finish Sub

  3. #3

    Re: read from text file, line by line

    Here is another way to do information technology. You lot don't demand to keep the file open all the time. Not unless you desire to write something back to it...

    Lawmaking:

    Sub ReadTextFile()     Dim TextFileData As Cord, MyArray() As String, I as Long                                                                      '~~> Open file every bit binary                                                                    Open "SampleText.Txt" For Binary Equally #1                                                                      '~~> Read unabridged file's data in one go                                                                    TextFileData = Infinite$(LOF(ane))     Go #1, , TextFileData                                                                      '~~> Close File                                                                    Close #i                                                                      '~~> Split the data in seperate lines                                                                    MyArray() = Split(TextFileData, vbCrLf)          For I = 0 To UBound(MyArray())                                                                      '~~> Replace this with your code         '~~> to interact with each line of data                                                                    MsgBox MyArray(I)     Side by side Terminate Sub

    A good do for the Center is to bend down and help another upwardly...
    Please Mark your Thread "Resolved", if the query is solved

    MyGear :
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ 1000. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX Black 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-grand-Farthermost yard Watt 80 Plus Golden Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC Twoscore ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gilt Keyboard
    ★ Mouse ★ Logitech G502 Hero


  4. #4

    Re: read from text file, line by line

    And some other style...if you loaded the text file into a ListBox, then each individual line would become its own individual ListBox item which could exist dealt with at your conveinance.

  5. #5

    nickpenny is offline

    New Fellow member


    Re: read from text file, line past line

    Quote Originally Posted past Hack View Post

    And some other way...if you lot loaded the text file into a ListBox, so each private line would go its own individual ListBox item which could be dealt with at your conveinance.

    I am trying to achieve the aforementioned matter and have got quite a messy text file in a list box. I need to interrogate it a line at a time and ask things similar, what is the boilerplate of sure numbers that come after certain words. Is this possible using the list box method and practice yous have whatsoever examples of this type of lawmaking?

  6. #half-dozen

    Re: read from text file, line by line

    Nick, welcome to the forum,
    it is posible past looping the list of listbox, it'due south better u post ur question as a new thread with proper title.

  7. #vii

    Re: read from text file, line by line

    Code:

    Public Role FilterGISFile(InputFile Equally String, OutputFile As Cord, _                               NSMin Equally Double, NSMax Equally Double, _                               EWMin As Double, EWMax As Double, Optional Suspend As Boolean = Truthful)                                                                   If (Not Append And (Dir(OutputFile) <> "")) Then Call Kill(OutputFile)          Dim IFN&: IFN = FreeFile: Open up InputFile For Input As IFN     Dim OFN&: OFN = FreeFile: Open OutputFile For Suspend Equally OFN          Dim CurLine$, Columns$()          Do While Not EOF(IFN)         Line Input #IFN, CurLine         If (Len(CurLine)) And then             Columns = Split(CurLine, ",")             If (UBound(Columns) >= two) Then                 Dim ns As Double, ew As Double                 ns = CDbl(Columns(0))                 ew = CDbl(Columns(1))                                  If (ns >= NSMin And ns <= NSMax And _                     ew >= EWMin And ew <= EWMax) Then _                     Print #OFN, CurLine             Terminate If         End If     Loop          Close #IFN     Shut #OFN      Cease Function

  8. #eight

    Re: read from text file, line by line

    Siddharth,
    I have blackness boxed your code a wee bit more than, so as to compare with what I have been using.
    Can you lot (or someone) comment of the benefits of either arroyo ?

    Code:

    Private Sub Command1_Click()  Dim I As Long  Dim sFileContents As String  Dim arrStr() Equally String  Dim sFilePath Equally String     sFilePath = App.Path & "\" & "SampleText.Txt"     sFileContents = GetFileContents(sFilePath)     '~~> Split the information in seperate lines     arrStr() = Split(sFileContents, vbCrLf)     For I = 0 To UBound(arrStr())         '~~> Supersede this with your code         '~~> to interact with each line of data         MsgBox arrStr(I)     Side by side Stop Sub  Individual Function GetFileContents(sFilePath As String) As Cord  Dim sData As String     '~~> Open file as binary     Open up sFilePath For Binary As #1     '~~> Read entire file's data in one go     sData = Space$(LOF(ane))     Get #one, , sData     '~~> Close File     Close #1     GetFileContents = sData End Function  Individual Part GetFileContents(ByVal sFilePath As String) As Cord  Dim fnum As Integer     fnum = FreeFile     Open up sFilePath For Input As fnum     GetFileContents = Input(LOF(fnum), fnum)     Close fnum Cease Office

Posting Permissions

  • You may non post new threads
  • You lot may not post replies
  • Yous may not post attachments
  • You may non edit your posts
  • BB code is On
  • Smilies are On
  • [IMG] code is On
  • [VIDEO] code is On
  • HTML code is Off

Click Here to Expand Forum to Total Width

selvidgewrid1989.blogspot.com

Source: https://www.vbforums.com/showthread.php?615580-read-from-text-file-line-by-line

0 Response to "Visual Studio Visual Basic Read File Line by Line"

Enviar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel