0

i am using VB.NET 2003 and i need to read some data from a cvs file and store it into a two dimensional array.... i couldn't find an example anywhere.. pls help :'(

4
Contributors
7
Replies
8
Views
7 Years
Discussion Span
Last Post by Teme64
0

See How to read csv file contents in VB.Net. It reads a csv file lines in to an array. There's comment at the end, how to get (comma) separate values from one line. Use the code in that thread and add a second loop to split lines and finally put data in to a two-dimensional array.

I won't post any code now. Try to do it first by yourself. If you encounter a problem, post your code and I'll help you. It's not that difficult task ;)

0
Public Class FormStart

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim readText() As String = System.IO.File.ReadAllLines("C:\file.txt")
		Dim s As String
		For Each s In readText
			Dim columns() As String = s.Split(New [Char]() {","c})
			System.Diagnostics.Debugger.Break()
		Next
	End Sub
End Class
0

The OP wanted csv file to a matrix. Here's sknake's code slightly modified

Dim Matrix(,) As String

Dim readText() As String = System.IO.File.ReadAllLines("C:\file.txt")
' Redim matrix
ReDim Matrix(readText.GetUpperBound(0), Strings.Split(readText(0), ",").GetUpperBound(0))

Dim s As String
Dim Line As Integer = 0
Dim j As Integer
For Each s In readText
  Dim columns() As String = s.Split(New [Char]() {","c})
  ' Copy one line to matrix
  For j = 0 To columns.GetUpperBound(0)
    Matrix(Line, j) = columns(j)
  Next j
  Line += 1
  System.Diagnostics.Debugger.Break()
Next

The code works with "well-formed" csv files but crashes if csv file has some oddities. There should be bounds checking added to the code.

0

i have used the code that you have provited but it gives me an errror... it sais: ReadAllLines is not a member of 'System.IO.File'

0

i have used the code that you have provited but it gives me an errror... it sais: ReadAllLines is not a member of 'System.IO.File'

Use System.IO.StreamReader class.

0

What version of the framework are you using? That method was implemented in 2.0 so if you are using 1.x then you probably need another approach as adatapost suggested

0

i am using VB.NET 2003

Sorry, my mistake. Here's 1.1 compatible version

Dim Matrix(,) As String

' .NET 2.0
'Dim readText() As String = System.IO.File.ReadAllLines("C:\file.txt")
' .NET 1.1
Dim readText() As String
Dim OneLine As String
Dim Lines As Integer
Lines = 0
Dim oReader As StreamReader = New StreamReader("C:\file.txt")
OneLine = oReader.ReadLine
Do Until OneLine Is Nothing
  ReDim Preserve readText(Lines)
  readText(Lines) = OneLine
  Lines += 1
  OneLine = oReader.ReadLine
Loop

' If matrix is empty, exit sub in here

' Redim matrix
ReDim Matrix(readText.GetUpperBound(0), Strings.Split(readText(0), ",").GetUpperBound(0))

Dim s As String
Dim Line As Integer = 0
Dim j As Integer
For Each s In readText
  Dim columns() As String = s.Split(New [Char]() {","c})
  ' Copy one line to matrix
  For j = 0 To columns.GetUpperBound(0)
    Matrix(Line, j) = columns(j)
  Next j
  Line += 1
  System.Diagnostics.Debugger.Break()
Next
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.