Read Each Line in a File Vb

  1. #1

    coder4life is offline

    Thread Starter

    Lively Member


    read from text file, line past line

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

    let's say the file is this

    12345 firm
    3245 mouse
    467 open door

    what i want is to keep the text file open, read the first line, practise something with it, then become to the next line, do something with it, etc, until i have procesed all the lines in the text file.

    thanks for any ideas


  2. #2

    Re: read from text file, line by line

    Code:

    Individual Sub ReadLines() Dim MyLine As String Open "C:\MyFile.txt" For Input As #1     Do While Not EOF(1)         Line Input #one, MyLine         MsgBox MyLine     Loop Close #1 End Sub

  3. #iii

    Re: read from text file, line by line

    Here is another way to do it. Yous don't need to keep the file open up all the time. Not unless y'all want to write something back to information technology...

    Lawmaking:

    Sub ReadTextFile()     Dim TextFileData Every bit String, MyArray() Every bit String, I as Long                                                                      '~~> Open file as binary                                                                    Open up "SampleText.Txt" For Binary As #i                                                                      '~~> Read entire file's data in one go                                                                    TextFileData = Space$(LOF(1))     Get #1, , TextFileData                                                                      '~~> Close File                                                                    Close #1                                                                      '~~> Separate the information in seperate lines                                                                    MyArray() = Split(TextFileData, vbCrLf)          For I = 0 To UBound(MyArray())                                                                      '~~> Replace this with your lawmaking         '~~> to collaborate with each line of data                                                                    MsgBox MyArray(I)     Adjacent End Sub

    A good exercise for the Eye is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved

    MyGear :
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. 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 Blackness 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-one thousand-EXTREME m Watt lxxx Plus Gold Fully Modular PSU
    ★ Instance ★ LIAN LI PC-O11 DYNAMIC XL ROG (Blackness) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero


  4. #4

    Re: read from text file, line by line

    And another manner...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 Member


    Re: read from text file, line by line

    Quote Originally Posted by Hack View Post

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

    I am trying to achieve the aforementioned thing and accept got quite a messy text file in a listing box. I demand to interrogate it a line at a time and inquire things like, what is the average of certain numbers that come up later on certain words. Is this possible using the listing box method and exercise you have whatsoever examples of this type of lawmaking?

  6. #6

    Re: read from text file, line by line

    Nick, welcome to the forum,
    information technology is posible by looping the list of listbox, information technology's improve u post ur question as a new thread with proper championship.

  7. #7

    Re: read from text file, line by line

    Code:

    Public Function FilterGISFile(InputFile As String, OutputFile Every bit Cord, _                               NSMin As Double, NSMax As Double, _                               EWMin As Double, EWMax Every bit Double, Optional Suspend As Boolean = True)                                                                   If (Not Append And (Dir(OutputFile) <> "")) Then Call Kill(OutputFile)          Dim IFN&: IFN = FreeFile: Open InputFile For Input As IFN     Dim OFN&: OFN = FreeFile: Open OutputFile For Append As OFN          Dim CurLine$, Columns$()          Do While Not EOF(IFN)         Line Input #IFN, CurLine         If (Len(CurLine)) And then             Columns = Dissever(CurLine, ",")             If (UBound(Columns) >= 2) And then                 Dim ns Equally Double, ew Equally Double                 ns = CDbl(Columns(0))                 ew = CDbl(Columns(i))                                  If (ns >= NSMin And ns <= NSMax And _                     ew >= EWMin And ew <= EWMax) Then _                     Print #OFN, CurLine             Finish If         End If     Loop          Close #IFN     Close #OFN      Stop Function

  8. #eight

    Re: read from text file, line by line

    Siddharth,
    I accept black boxed your code a wee flake more, so equally to compare with what I accept been using.
    Can yous (or someone) comment of the benefits of either approach ?

    Code:

    Private Sub Command1_Click()  Dim I Every bit Long  Dim sFileContents As String  Dim arrStr() As Cord  Dim sFilePath Equally String     sFilePath = App.Path & "\" & "SampleText.Txt"     sFileContents = GetFileContents(sFilePath)     '~~> Divide the data in seperate lines     arrStr() = Dissever(sFileContents, vbCrLf)     For I = 0 To UBound(arrStr())         '~~> Replace this with your lawmaking         '~~> to interact with each line of data         MsgBox arrStr(I)     Next End Sub  Private Office GetFileContents(sFilePath As Cord) As String  Dim sData Equally Cord     '~~> Open file as binary     Open sFilePath For Binary As #1     '~~> Read unabridged file'due south data in one go     sData = Infinite$(LOF(i))     Get #1, , sData     '~~> Close File     Close #ane     GetFileContents = sData End Office  Individual Office GetFileContents(ByVal sFilePath As String) Equally String  Dim fnum As Integer     fnum = FreeFile     Open sFilePath For Input Every bit fnum     GetFileContents = Input(LOF(fnum), fnum)     Shut fnum Terminate Function

Posting Permissions

  • You lot may not mail new threads
  • You may not postal service replies
  • You may not post attachments
  • Yous may not 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

Read Each Line in a File Vb

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

0 Response to "Read Each Line in a File Vb"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel