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 read data into array from textfile?

How to read data into array from textfile?

Scheduled Pinned Locked Moved Visual Basic
data-structurestestingbeta-testingtutorialquestion
11 Posts 2 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.
  • T TeiUKei

    I try to read data into array and display the data, however i only manage to get 1 in return. Below is my coding, may i have some suggestion and advice? Option Strict On Imports System.IO Public Class Form1 Private Function Language(ByVal MLanguage As Integer, ByVal RequestDataID() As Integer, ByRef GetBackWord() As String) As String Dim s As String = String.Empty Dim Number As Integer = 0 Dim sFileName As String If MLanguage = 1 Then sFileName = "C:\testing.txt" Try Dim oFile As FileStream = New FileStream(sFileName, FileMode.Open, FileAccess.Read, FileShare.Read) Dim oReader As StreamReader = New StreamReader(oFile) Dim a As Integer = 0 Do Until (Number = RequestDataID(a)) If Number < RequestDataID(a) Then s = oReader.ReadLine() Language = s 'GetBackWord(6) = s Number += 1 a += 1 Else s = oReader.ReadLine() End If Loop oReader.Close() oFile.Close() Catch GetBackWord(0) = "a." End Try ElseIf MLanguage = 2 Then sFileName = "C:\Maklumat.txt" Try Dim oFile As FileStream = New FileStream(sFileName, FileMode.Open, FileAccess.Read, FileShare.Read) Dim oReader As StreamReader = New StreamReader(oFile) Dim a As Integer = 0 Do Until (Number = RequestDataID(a)) If Number < RequestDataID(a) Then s = oReader.ReadLine() GetBackWord(a) = s Number += 1 a += 1 Else s = oReader.ReadLine() End If Loop oReader.Close() oFile.Close() Catch GetBackWord(0) = "a" End Try Else sFileName = "C:\CMsg.txt" Try Dim oFile As FileStream = New FileStream(sFileName, FileMode.Open, FileAccess.Read, FileShare.Read) Dim oReader As StreamReader = New StreamReader(oFile) 'Dim a As Integer =

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

    TeiUKei wrote:

    s = oReader.ReadLine() Language = s

    This seems redundant. Either way, your return value is being changed every time, not accumulated. Is that the intention ?

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

    T 1 Reply Last reply
    0
    • C Christian Graus

      TeiUKei wrote:

      s = oReader.ReadLine() Language = s

      This seems redundant. Either way, your return value is being changed every time, not accumulated. Is that the intention ?

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

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

      No, my intention is for example, everytime i receive a requestID(3), i will read 4 lines from the textfile and store in the array, GetBackWord(3) and display the lines i get. Any suggestion? I stuck there and be so appreciate for help.

      C 1 Reply Last reply
      0
      • T TeiUKei

        No, my intention is for example, everytime i receive a requestID(3), i will read 4 lines from the textfile and store in the array, GetBackWord(3) and display the lines i get. Any suggestion? I stuck there and be so appreciate for help.

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

        I'm not really sure what you mean. The code looks messy - perhaps if you told us what's in the file, and what you expect the function to return ( and what you're getting instead ) ?

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

        T 1 Reply Last reply
        0
        • C Christian Graus

          I'm not really sure what you mean. The code looks messy - perhaps if you told us what's in the file, and what you expect the function to return ( and what you're getting instead ) ?

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

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

          Well, this is the data in the file No,Title 1,Error Message Information Question a b c d e f Let say i receive RequestDataID = {1,2,3,4,5,6,7}, then for RequestDataID(0),line 1 will be read and stored in GetBackWord(0) which then send back to the function called and same with the other. However, i only manage to get c in return. What should i suppose to do?

          C 1 Reply Last reply
          0
          • T TeiUKei

            Well, this is the data in the file No,Title 1,Error Message Information Question a b c d e f Let say i receive RequestDataID = {1,2,3,4,5,6,7}, then for RequestDataID(0),line 1 will be read and stored in GetBackWord(0) which then send back to the function called and same with the other. However, i only manage to get c in return. What should i suppose to do?

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

            I honestly can't work out how this code is supposed to work. Really, what you should do is read the entire file into an array, File.ReadAllLines (from memory ) does this. Then you can copy whatever lines you want. Other suggestion - set a breakpoint and step through your code to try and work out where it's going wrong.

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

            T 1 Reply Last reply
            0
            • C Christian Graus

              I honestly can't work out how this code is supposed to work. Really, what you should do is read the entire file into an array, File.ReadAllLines (from memory ) does this. Then you can copy whatever lines you want. Other suggestion - set a breakpoint and step through your code to try and work out where it's going wrong.

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

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

              Well, thanx for help anyway!!

              C 1 Reply Last reply
              0
              • T TeiUKei

                Well, thanx for help anyway!!

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

                I was hoping we might dig a little deeper. Does File.ReadAllLines ( or whatever it is ) help ?

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

                T 1 Reply Last reply
                0
                • C Christian Graus

                  I was hoping we might dig a little deeper. Does File.ReadAllLines ( or whatever it is ) help ?

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

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

                  I checked my code and wondered if it was because RichTextbox only take the recent memory, that's why it only displayed one of it.

                  C 1 Reply Last reply
                  0
                  • T TeiUKei

                    I checked my code and wondered if it was because RichTextbox only take the recent memory, that's why it only displayed one of it.

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

                    The rich text box will show exactly what you ask it to. If the function returns what you expect, then the problem is how you assign it to the rich text box.

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

                    T 1 Reply Last reply
                    0
                    • C Christian Graus

                      The rich text box will show exactly what you ask it to. If the function returns what you expect, then the problem is how you assign it to the rich text box.

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

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

                      Well, i solve the problem of display data. Thanx!!!

                      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