Results 1 to 11 of 11

Thread: Read file and assign to variable

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    9

    Read file and assign to variable

    I understand how to read a file but I'm unsure on how to assign each line of text in the file to an array variable. I've attempted many times but when I assign it to a label, it's completely empty.

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Read file and assign to variable

    What code do you have so far?

    Pseudocode would be something like this:

    Code:
    Dim strLinesFromFile() as String
    Dim intIndex As Integer = 0
    
    Open File
    Do Until EOF
      Redim Preserve strLinesFromFile(intIndex)
      Read Line From File Into strLinesFromFile(intIndex)
      intIndex +=1
    Loop
    Close File
    That would give you an array that contains each line from the input file.

    Assigning the values in the array to a label would need more work than simply:

    Label1.Text = strLinesFromFile

    since strLinesFromFile is an array, so you would need a loop that appends each item from the strLinesFromFile array to Label1.Text

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    9

    Re: Read file and assign to variable

    Below I have just used the code to try and read the first line to test if it shows up on the form, but it doesn't.

    Dim Reader As IO.StreamReader
    Dim StringReader As String

    Reader = New IO.StreamReader("Answers.txt")
    StringReader = Console.ReadLine

    Label1.Text = StringReader

  4. #4
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Read file and assign to variable

    Quote Originally Posted by RandomAccount11 View Post
    Below I have just used the code to try and read the first line to test if it shows up on the form, but it doesn't.

    Dim Reader As IO.StreamReader
    Dim StringReader As String

    Reader = New IO.StreamReader("Answers.txt")
    StringReader = Console.ReadLine

    Label1.Text = StringReader
    Doesn't Console.ReadLine get keyboard I/O from a Console program? I think you need to use a different command to read from a file.

  5. #5

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    9

    Re: Read file and assign to variable

    What different command do you suggest?

  6. #6
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Read file and assign to variable

    Quote Originally Posted by RandomAccount11 View Post
    What different command do you suggest?
    Well, you are creating the Reader object but then you aren't using it for anything. You should use that object to read from the file, like Reader.ReadLine

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Read file and assign to variable

    If you want to populate an array with the lines from a file then do this:
    vb.net Code:
    1. Dim lines = IO.File.ReadAllLines(filePath)
    The ReadAllLines method handles the StreamReader itself internally.

  8. #8

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    9

    Re: Read file and assign to variable

    Thank you. The code below works and shows up in the label. However, the entire text file shows up in the label. For example, it shows 6 lines of "right". Is there a way I can select each line seperately, line by line, and assign it to a variable?

    Dim Text As String
    Dim Reader As IO.StreamReader
    Dim StringReader As String

    Reader = New IO.StreamReader("Answers.txt")
    StringReader = Reader.ReadToEnd
    Text = StringReader

    Label1.Text = Text

  9. #9
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Read file and assign to variable

    In the earlier code you were using .ReadLine, now you switched to .ReadToEnd. As these names indicate, .ReadLine reads one line of data from a file, and .ReadToEnd reads all data from where the current file pointer is located until the end of the file is reached. Since you are calling .ReadToEnd immediately after opening the file, the file pointer is at the beginning of the file and the result is that the entire file is returned to StringReader.

  10. #10

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    9

    Re: Read file and assign to variable

    I'm basically trying to put these 'right' and 'wrong' strings into a results table. I need to assign these 6 strings into their designated rows. So I need:

    Label1.text = first line of text file
    Label2.text = second line
    so on
    so on ...

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Read file and assign to variable

    In post #1 you specifically said that you wanted to use an array. Is that correct or not? If all you want to do is to read six lines into six Labels then go ahead. Create a StreamReader and call ReadLine six times, assigning the result to a different Label's Text each time. If you know how to do it for one line then you know how to do it for any number of lines.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width