tssweb

I have a text file in which I would like to read line by line and display each line one at a time in message boxes. Can anyone help Text file is below.

4
113
2
A1455
00320
025
1819
122006
155

I would like the output to read in each message box:

Line 1 = 4

Line 2 = 113

Line 3 = 2

etc.




Re: Visual Basic Express Edition Read a text file line by line.

ahmedilyas

sure.

import the System.IO namespace then...

Dim theReader as new StreamReader("filename.txt") 'create an object to read the file, the StreamReader reads streams/files

while theReader.EndOfStream = false 'while we have data and not reached to the end of the stream

   MessageBox.Show(theReader.ReadLine()) 'read the line we are on and show it in the messagebox

end while






Re: Visual Basic Express Edition Read a text file line by line.

tssweb

I want to read the entries one at a time and assign each entry to variable. Can this be done and how




Re: Visual Basic Express Edition Read a text file line by line.

ahmedilyas

well you can but it would be inefficient as you have to know the number of items in the text file, create the same amount of variables (you cant create variables on the fly) and then assign the item to that variable. you could alternatively store each line in an array either by using a strong typed collection or by an arraylist....

Dim theLines as new List<string>()

Dim theReader as new StreamReader("filename.txt") 'create an object to read the file, the StreamReader reads streams/files

while theReader.EndOfStream = false 'while we have data and not reached to the end of the stream

theLines.Add(theReader.ReadLine()) 'read the line we are on and show it in the messagebox

end while

and thats it. you can go through each item either via index or via a loop:

for each currentLine as String in theLines

MessageBox.Show(currentLine)

next






Re: Visual Basic Express Edition Read a text file line by line.

spotty

or you could read the whole file in one go and split it into each individual line

something like

Dim s As String = My.COmputer.Filesystem.ReadAllText("C:\test.txt")

Dim c() As Char = {vbCrLf}

Dim sa() As String = s.Split(c, StringSplitOptions.None)

For Each s1 As String In sa

Console.WriteLine(s1)

Next





Re: Visual Basic Express Edition Read a text file line by line.

tssweb

This does work when I just want the items in the file listed, but I would like to assign each listed item to a variable. When a loop is performed, there is only one variable in which the items are assigned.




Re: Visual Basic Express Edition Read a text file line by line.

tssweb

The text file is listed below. What I want to do is: take each entry in the text file and assign it to a variable.

text file:

4
113
2
A1455
00320
025
1819
122006
155

I would like to assign each entry in the text file to a variable:

4 = text1

113 = text2

2 = text3

A1455 = text4

00320 = text5

025 = text6

1819 = text7

122006 = text8

155 = text9

Then I am going to compare each variable to an input value entered by a user. The user will enter his/her information into a textbox, the application will compare the user info with the text file info, one at a time, and flash a message if there is any difference between the two. I hope this make sense.






Re: Visual Basic Express Edition Read a text file line by line.

AndrewVos

Streamreader.readline() reads a line of text from the file then advances to the next line.

text1=streamreader.readline

text2=streamreader.readline

This will work.






Re: Visual Basic Express Edition Read a text file line by line.

tssweb

Thanks.