How do you read data from a file and fill an array?

Recommended Answers

All 3 Replies

It's hard to tell, it bases on the type of file you are using. The common way was to fill a dataset with your data from your file, then fill data from dataset to your array.

This is the code I have so far, not sure if it'd work tho? Don't really understand the whole StreamReader part

Imports Systom. IO

Dim EmpData(99) As Employee (already made the structure for Employee)

Dim intCount As Integer
Dim strFileName As String
Dim DiskFile As StreamReader

StrFileName = InputBox("Entrer the FileName")

If File.Exists(DiskFile) Then

    Disk File = File.OpenText(strFileName.txt)

    For intCount = 0 to (EmpData.Length-1)
        EmpData(intCount) = Cstr(DiskFile.ReadLine())
    Next
    DiskFile.Close()
Else
    MessageBox.Show("The File does not exist")
End If

First a reminder to please insert code using the Code tool. This preserves formatting. You can avoid the messy details of IO by

If File.Exists(DiskFile) Then

    Dim intCount As Integer = 0

    For Each line As String In System.IO.File.ReadAllLines(strFileName.txt)
        EmpData(intCount) = Cstr(line)
        intCount += 1
        if intCount = EmpData.Length Then Exit For
    Next

Else
    MessageBox.Show("The File does not exist")
End If
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.