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. loading text from a file...

loading text from a file...

Scheduled Pinned Locked Moved Visual Basic
csshelp
5 Posts 4 Posters 1 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.
  • S Offline
    S Offline
    Sergi25
    wrote on last edited by
    #1

    Ok. Here is my scenarion I have a file that I need to load some line to a grid, but I'm have diffulties doing do. The following is the structure of my file. 0 62.5 0 0 62.5 0 0 62.5 0 0 'H2O' 0 115 461.5 461.5 115 461.5 461.5 115 461.5 461.5 'CH' 0 90 76.9 76.9 95 107.7 107.7 90 76.9 76.9 'CH' 0 97 76.9 76.9 97 107.7 107.7 97 76.9 76.9 'CH' 0 97 76.9 76.9 100 153.8 153.8 97 76.9 76.9 'CH' I need to assign each number to a variable to load into my grid. Help please.:(

    C G G 3 Replies Last reply
    0
    • S Sergi25

      Ok. Here is my scenarion I have a file that I need to load some line to a grid, but I'm have diffulties doing do. The following is the structure of my file. 0 62.5 0 0 62.5 0 0 62.5 0 0 'H2O' 0 115 461.5 461.5 115 461.5 461.5 115 461.5 461.5 'CH' 0 90 76.9 76.9 95 107.7 107.7 90 76.9 76.9 'CH' 0 97 76.9 76.9 97 107.7 107.7 97 76.9 76.9 'CH' 0 97 76.9 76.9 100 153.8 153.8 97 76.9 76.9 'CH' I need to assign each number to a variable to load into my grid. Help please.:(

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

      The System.IO namespace has the classes you need to read files.  I think that My gives you a shortcut as well.  System.IO.File.ReadAllLines will read your file into a string array, a string per line.  You can then call the Split method on the string class to turn each line into a string array ( split on space ).  Then double.TryParse can be used to find all the numbers, and you'll always know that the last element is a string, so treat that accordingly.

      Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert

      1 Reply Last reply
      0
      • S Sergi25

        Ok. Here is my scenarion I have a file that I need to load some line to a grid, but I'm have diffulties doing do. The following is the structure of my file. 0 62.5 0 0 62.5 0 0 62.5 0 0 'H2O' 0 115 461.5 461.5 115 461.5 461.5 115 461.5 461.5 'CH' 0 90 76.9 76.9 95 107.7 107.7 90 76.9 76.9 'CH' 0 97 76.9 76.9 97 107.7 107.7 97 76.9 76.9 'CH' 0 97 76.9 76.9 100 153.8 153.8 97 76.9 76.9 'CH' I need to assign each number to a variable to load into my grid. Help please.:(

        G Offline
        G Offline
        gladsmhe
        wrote on last edited by
        #3

        my be you should try file manipulation.. ex: While yourFileHandler.Peek <> -1 yourArray = split(yourFileHandler.Readline()," ") For i as Integer =0 to Ubound(yourArray) 'yourArray(i) do assigning the value of array here into your datagrid Next End While

        1 Reply Last reply
        0
        • S Sergi25

          Ok. Here is my scenarion I have a file that I need to load some line to a grid, but I'm have diffulties doing do. The following is the structure of my file. 0 62.5 0 0 62.5 0 0 62.5 0 0 'H2O' 0 115 461.5 461.5 115 461.5 461.5 115 461.5 461.5 'CH' 0 90 76.9 76.9 95 107.7 107.7 90 76.9 76.9 'CH' 0 97 76.9 76.9 97 107.7 107.7 97 76.9 76.9 'CH' 0 97 76.9 76.9 100 153.8 153.8 97 76.9 76.9 'CH' I need to assign each number to a variable to load into my grid. Help please.:(

          G Offline
          G Offline
          Geoff_3001
          wrote on last edited by
          #4

          Here is some bits of code (not complete) I have used to do somthing similar. 1) it opens the launch.ini file 2) creates a streamreader and puts the content of the file into infile 3) then it reads infile one char at a time building a temp string until it reads a comma (in your case it would be a space) 4) On reaching a comma it stores the value in a string (in the example IP and Mac) Hope this helps you get started. thefile = "c:\launch.ini" Try fs = New FileStream(thefile, FileMode.Open) Catch excep As System.IO.FileNotFoundException Exit Sub End Try Dim sr As StreamReader = New StreamReader(fs) Dim infile As String ' String used to accept file input Dim c As Char Dim count As Integer Dim temp As String infile = sr.ReadToEnd sr.Close() fs.Close() 'MessageBox.Show(infile) count = 0 Do c = infile.Substring(count, 1) count = count + 1 If c <> "," Then temp &= c Loop While c <> "," ip = temp ipbox.Text = ip temp = "" Do c = infile.Substring(count, 1) count = count + 1 If c <> "," Then temp &= c Loop While c <> "," mac = temp macbox.Text = mac temp = ""

          S 1 Reply Last reply
          0
          • G Geoff_3001

            Here is some bits of code (not complete) I have used to do somthing similar. 1) it opens the launch.ini file 2) creates a streamreader and puts the content of the file into infile 3) then it reads infile one char at a time building a temp string until it reads a comma (in your case it would be a space) 4) On reaching a comma it stores the value in a string (in the example IP and Mac) Hope this helps you get started. thefile = "c:\launch.ini" Try fs = New FileStream(thefile, FileMode.Open) Catch excep As System.IO.FileNotFoundException Exit Sub End Try Dim sr As StreamReader = New StreamReader(fs) Dim infile As String ' String used to accept file input Dim c As Char Dim count As Integer Dim temp As String infile = sr.ReadToEnd sr.Close() fs.Close() 'MessageBox.Show(infile) count = 0 Do c = infile.Substring(count, 1) count = count + 1 If c <> "," Then temp &= c Loop While c <> "," ip = temp ipbox.Text = ip temp = "" Do c = infile.Substring(count, 1) count = count + 1 If c <> "," Then temp &= c Loop While c <> "," mac = temp macbox.Text = mac temp = ""

            S Offline
            S Offline
            Sergi25
            wrote on last edited by
            #5

            Thank you guys

            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