Reading CSV files

R

RD

Need to read a comma separated values file, line by line and extract each
value from each line. There may be variable number of values in each of the
lines.

Anyone know of any sample code to do this?
Used to be easy in vb6 , oh well!

Thanks for any help,

Bob
 
C

Chris Dunaway

Need to read a comma separated values file, line by line and extract each
value from each line. There may be variable number of values in each of the
lines.

Anyone know of any sample code to do this?
Used to be easy in vb6 , oh well!

Thanks for any help,

Bob

It's easy in .Net too. Look at the classes in the System.IO namespace.
Here's a sample from memory (may have typos):

'\\\\\
Imports System.IO

Public Sub ReadCSVFile(fname As String)

Dim sr As StreamReader = File.OpenText(fname)

'Array to hold all of the comma delimited items
Dim aTokens() As String

Do While sr.Peek() >= 0
aTokens = sr.ReadLine().Split(',')
For x As Integer = 0 to aTokens.Length - 1
'Do something here with the token
Console.WriteLine(aTokens(x))
Next
Loop

sr.Close()

End Sub
'/////
 
R

RD

Thanks Chris
Chris Dunaway said:
It's easy in .Net too. Look at the classes in the System.IO namespace.
Here's a sample from memory (may have typos):

'\\\\\
Imports System.IO

Public Sub ReadCSVFile(fname As String)

Dim sr As StreamReader = File.OpenText(fname)

'Array to hold all of the comma delimited items
Dim aTokens() As String

Do While sr.Peek() >= 0
aTokens = sr.ReadLine().Split(',')
For x As Integer = 0 to aTokens.Length - 1
'Do something here with the token
Console.WriteLine(aTokens(x))
Next
Loop

sr.Close()

End Sub
'/////
--
Chris

To send me an E-mail, remove the underscores and lunchmeat from my E-Mail
address.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top