1.11M Members

Visual Basic reading text file into array

 
0
 

hi,
i am trying to read from a text file and store the data in an array but i don't know how to get started with my code. can someone please get me started here.

i have start with the code for locating the file, but i am having difficulty reading the file.

Dim strFileName As String
OpenAnswerFile.ShowDialog()
strFileName = OpenAnswerFile.FileName
MessageBox.Show(strFileName) 'message box showing the file path

 
0
 

See if this helps.

'Dim OpenAnswerFile As New OpenFileDialog
        Dim strFileName() As String '// String Array.
        Dim tempStr As String = "" '// temp String for result.

        If OpenAnswerFile.ShowDialog = DialogResult.OK Then '// check if OK was pressed.
            strFileName = IO.File.ReadAllLines(OpenAnswerFile.FileName) '// add each line as String Array.
            For Each myLine In strFileName '// loop thru Arrays.
                tempStr &= myLine & vbNewLine '// add Array and new line.
            Next
            MsgBox(tempStr) '// display result.
        End If
 
0
 

Hi,

Thanks for the feedback, it seems to work but the problem is it is not displaying the right results. i am trying to display this characters TFTTTTTTFFTFTFTTTTFF
To you why this is happening..

Thanks

 
1
 

Not so clear on your reply.
Does your File only contain one line of "TFTTTTTTFFTFTFTTTTFF"?, and are you trying to separate each character into an array?

 
0
 

yes my file only contain this line of code "TFTTTTTTFFTFTFTTTTFF" .
What i am trying to do is read and store it in an array of Strings and then display the result.

 
0
 
Dim myCoolFile As String = "C:\test.txt" '// your file location.
        Dim tempStr As String = IO.File.ReadAllText(myCoolFile) '// read file into a String.
        Dim x As Integer = tempStr.Length - 1 '// get String length, Index based, starting at 0 not 1.
        Dim myArray(x) As String '// create your String Arrays.
        Dim i As Integer = 0 '// Integer used to increase Array #'s.
        For Each myChr As Char In tempStr '// loop thru each each character in the String.
            myArray(i) = myChr '// add values to each String Array.
            i += 1 '// increase count for next Array.
        Next
        MsgBox(myArray(2)) '// display result for 3rd character.
 
0
 

Thanks very much, appreciate your help

Question Answered as of 3 Years Ago by codeorder
 
-1
 

I am trying to do something almost exactly like this, but i want to load multiple characters. The array is being set-up by a prompt. The variables student and addstudent are defined in this block of code:
Dim Prompt, Title As String
Dim i As Short
Prompt = "Enter your students' names."
AddStudent = InputBox("How many students are in your class?")
If AddStudent > 0 Then ReDim Student(AddStudent)
Student(0) = AddStudent
For i = 1 To UBound(Student)
Title = "Student " & (i + 1)
Student(i) = InputBox(Prompt, Title)
Next
How do i save this then open it so that i can use the defined variables?
Any idea on how to do this?

 
0
 

ng5, start your own thread and Flag.Post your own dumb.a.ss after.:D

send me p.m. w/link to thread.after, I might be able to help.:)

 
0
 

Hi...I had a vb question here.....now i want compare input staff id with text file store id...so how i going to compare multiple staff id in text file?

 
0
 

This thread has been solved. Please start a new thread with an appropriate title and more detail than you provided here.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: