Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. How to select specific data from textfile

How to select specific data from textfile

Scheduled Pinned Locked Moved Visual Basic
questionhelptestingbeta-testingtutorial
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TeiUKei
    wrote on last edited by
    #1

    Let say i have the data below in a textfile call testing.txt No,Title 1,Information 2,Error message 3,Question Now, how can i specify,get and display the second line data?:confused: I would appreciate so much for help!!

    C 1 Reply Last reply
    0
    • T TeiUKei

      Let say i have the data below in a textfile call testing.txt No,Title 1,Information 2,Error message 3,Question Now, how can i specify,get and display the second line data?:confused: I would appreciate so much for help!!

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      If it's the second line, you can read a line at a time and use the second one. Or you can use a regex to parse the data.

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

      T 1 Reply Last reply
      0
      • C Christian Graus

        If it's the second line, you can read a line at a time and use the second one. Or you can use a regex to parse the data.

        Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

        T Offline
        T Offline
        TeiUKei
        wrote on last edited by
        #3

        Thanks for help, but can i have sample coding?

        C 1 Reply Last reply
        0
        • T TeiUKei

          Thanks for help, but can i have sample coding?

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          System.IO.File.ReadXXX is the group of methods that are the easiest way to read a file into a string or group of strings.

          Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

          T 1 Reply Last reply
          0
          • C Christian Graus

            System.IO.File.ReadXXX is the group of methods that are the easiest way to read a file into a string or group of strings.

            Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

            T Offline
            T Offline
            TeiUKei
            wrote on last edited by
            #5

            Sorry for the troublesome! This is some of my code which i only manage to get title as return. May i have some advice on it? Private Function ReadTextFile(ByVal sFileName As String) As String Dim s As String = String.Empty Try Dim oFile As FileStream = New FileStream(sFileName, FileMode.Open, FileAccess.Read, FileShare.Read) Dim oReader As StreamReader = New StreamReader(oFile) s = oReader.ReadLine() oReader.Close() oFile.Close() ReadTextFile = s Catch ReadTextFile = "Unable to open file." End Try End Function

            S 1 Reply Last reply
            0
            • T TeiUKei

              Sorry for the troublesome! This is some of my code which i only manage to get title as return. May i have some advice on it? Private Function ReadTextFile(ByVal sFileName As String) As String Dim s As String = String.Empty Try Dim oFile As FileStream = New FileStream(sFileName, FileMode.Open, FileAccess.Read, FileShare.Read) Dim oReader As StreamReader = New StreamReader(oFile) s = oReader.ReadLine() oReader.Close() oFile.Close() ReadTextFile = s Catch ReadTextFile = "Unable to open file." End Try End Function

              S Offline
              S Offline
              Steve
              wrote on last edited by
              #6

              Your problem is that you're only reading the first line of the file. The ReadLine call needs to be a recurrent call, based on the return value of the method. e.g. (from MSDN) Dim input As String input = sr.ReadLine() While Not input Is Nothing 'Do something with the input 'Read the next line of input input = sr.ReadLine() End While This will continue to read the file until there is no further input returned by the ReadLine() call. Then you'll have to compare the returned input string to whatever you want to catch, either using a regex or a string constant (in the case of a marker string or something that won't change) to get the particular peice of info you're after. I would add do it something like this... Dim input As String input = sr.ReadLine() While Not input Is Nothing If (input.CompareTo(stringConstant) = 0) 'do whatever with the data input = Nothing Else input = sr.ReadLine() End If End While

              T 1 Reply Last reply
              0
              • S Steve

                Your problem is that you're only reading the first line of the file. The ReadLine call needs to be a recurrent call, based on the return value of the method. e.g. (from MSDN) Dim input As String input = sr.ReadLine() While Not input Is Nothing 'Do something with the input 'Read the next line of input input = sr.ReadLine() End While This will continue to read the file until there is no further input returned by the ReadLine() call. Then you'll have to compare the returned input string to whatever you want to catch, either using a regex or a string constant (in the case of a marker string or something that won't change) to get the particular peice of info you're after. I would add do it something like this... Dim input As String input = sr.ReadLine() While Not input Is Nothing If (input.CompareTo(stringConstant) = 0) 'do whatever with the data input = Nothing Else input = sr.ReadLine() End If End While

                T Offline
                T Offline
                TeiUKei
                wrote on last edited by
                #7

                Thank u, it really help a lot!:)

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups