none
Read data from a txt file(notepad) RRS feed

  • Question

  • Guys, i need to read data from text document (notepad),  this data have 2 columns, starting from the second line, i dont know how much lines of data will be, so probably i have to use "do while" ou "do until", as long i have data, until the end of the file. And after reading the data, write this data into variables to be used later. Can someone give some insight of a code to do that?

    And i another thing, in the first line, have a data too, and this data will be needed to another variable.



    Friday, July 5, 2019 3:08 AM

All replies

  • Hi Gustavo,
    try this demo:

    Imports System.IO
    
    Module Module20
      Sub Main()
        Try
          Call (New Demo).Execute()
        Catch ex As Exception
          Console.WriteLine(ex.ToString)
        End Try
        Console.WriteLine("Continue enter key")
        Console.ReadKey()
      End Sub
    
      Friend Class Demo
    
        Friend Sub Execute()
          Using rdr As New StreamReader("Module20TextFile1.txt")
            rdr.ReadLine()
            While Not rdr.EndOfStream
              Dim line = rdr.ReadLine
              Dim columns = Split(line, vbTab)
              If columns.Count = 2 Then
                Console.WriteLine($"{columns(0)} - {columns(1)}")
              Else
                Dim test = Join(columns, " - ")
                Console.WriteLine($"invalid data: {test}")
              End If
            End While
          End Using
        End Sub
    
      End Class
    
    End Module


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Friday, July 5, 2019 3:38 AM