Start New Discussion within our Software Development Community

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
strFileName = OpenAnswerFile.FileName
MessageBox.Show(strFileName) 'message box showing the file path

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.
            MsgBox(tempStr) '// display result.
        End If


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..


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?

BONEKA FULL BODY ELECTRIC ini terbuat dari bahan pilihan yang lembut, fleksibel dan berkualitas sehingga tidak menimbulkan penyakit dan iritasi pada kulit. UKURAN : panjang : 165cm berat : 4kg BONEKA FULL BODY ELECTRIC dapat di pompa dan di kemas, d

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.

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.
        MsgBox(myArray(2)) '// display result for 3rd character.

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)

How do i save this then open it so that i can use the defined variables?
Any idea on how to do this?

Comments or just.because?:)

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

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

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

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

Dim myFSO As Scripting.FileSystemObject
Dim myTextStream As Scripting.TextStream
Dim myString As String

Set myFSO = New Scripting.FileSystemObject

If myFSO.FileExists("filepath.txt") Then
Set myTextStream = myFSO.OpenTextFile("filepath.txt", ForReading)
myString = myTextStream.ReadAll() ' file content will be available in myString
Call myTextStream.Close
End If

Set myTextStream = Nothing
Set myFSO = Nothing

Refer this link for more details

This question has already been answered. Start a new discussion instead.