-
Dec 17th, 2017, 04:46 PM
#1
Thread Starter
New Member
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.
-
Dec 17th, 2017, 04:58 PM
#2
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
-
Dec 17th, 2017, 05:10 PM
#3
Thread Starter
New Member
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
-
Dec 17th, 2017, 05:13 PM
#4
Re: Read file and assign to variable
Originally Posted by RandomAccount11
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.
-
Dec 17th, 2017, 05:17 PM
#5
Thread Starter
New Member
Re: Read file and assign to variable
What different command do you suggest?
-
Dec 17th, 2017, 05:26 PM
#6
Re: Read file and assign to variable
Originally Posted by RandomAccount11
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
-
Dec 17th, 2017, 07:00 PM
#7
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:
Dim lines = IO.File.ReadAllLines(filePath)
The ReadAllLines method handles the StreamReader itself internally.
-
Dec 17th, 2017, 07:24 PM
#8
Thread Starter
New Member
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
-
Dec 17th, 2017, 08:01 PM
#9
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.
-
Dec 17th, 2017, 08:14 PM
#10
Thread Starter
New Member
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 ...
-
Dec 17th, 2017, 08:38 PM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|