473,403 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Simple VB6 read and write converted to vb.net? How to Read CSV????

I think VB.NET drives some people crazy because some simple VB6 things
seem so hard.

Here is some VB6 code:

'Write CSV File
open "c:\test.csv" for output as #1
write#1, "1","2","3","4","5"
write#1, "1","2","3","4","5"
write#1, "1","2","3","4","5"
Close#1

'Read CSV File
open "c:\test.csv" for input as #1
input#1, A$,B$,C$,D$,E$
input#1, A$,B$,C$,D$,E$
input#1, A$,B$,C$,D$,E$
Close#1

Works perfect and real easy. I can also open up the file in excel.

In VB.NET I can write the file the same way with:

FileOpen(1, "c:\testfile2.csv", OpenMode.Output)
WriteLine(1, "1", "2", "3", "4")
WriteLine(1, "2", "2", "3", "4")
WriteLine(1, "3", "2", "3", "4")
FileClose(1)

Now, how can I easily read the file from VB.NET into variables???? It
is driving me crazy and there seems to be about 10 different ways but
I want a simple CSV read routine. THANKS for any help.

Sheila

Sep 4 '07 #1
5 3195
vb.net is high Tech.
<ne**********@gmail.comwrote in message
news:11**********************@o80g2000hse.googlegr oups.com...
>I think VB.NET drives some people crazy because some simple VB6 things
seem so hard.

Here is some VB6 code:

'Write CSV File
open "c:\test.csv" for output as #1
write#1, "1","2","3","4","5"
write#1, "1","2","3","4","5"
write#1, "1","2","3","4","5"
Close#1

'Read CSV File
open "c:\test.csv" for input as #1
input#1, A$,B$,C$,D$,E$
input#1, A$,B$,C$,D$,E$
input#1, A$,B$,C$,D$,E$
Close#1

Works perfect and real easy. I can also open up the file in excel.

In VB.NET I can write the file the same way with:

FileOpen(1, "c:\testfile2.csv", OpenMode.Output)
WriteLine(1, "1", "2", "3", "4")
WriteLine(1, "2", "2", "3", "4")
WriteLine(1, "3", "2", "3", "4")
FileClose(1)

Now, how can I easily read the file from VB.NET into variables???? It
is driving me crazy and there seems to be about 10 different ways but
I want a simple CSV read routine. THANKS for any help.

Sheila

Sep 4 '07 #2
Your VB6 reader has a built in assumption about the number of columns in a
row of data. For example, your code needs to change if you want more data.
In general I would read the lines into an array of lines with
File.ReadAllLines, then split each row into colums with the string.Split()
method. If you know there are a lot of lines, then maybe reading one line at
a time will suffice.

"ne**********@gmail.com" wrote:
I think VB.NET drives some people crazy because some simple VB6 things
seem so hard.

Here is some VB6 code:

'Write CSV File
open "c:\test.csv" for output as #1
write#1, "1","2","3","4","5"
write#1, "1","2","3","4","5"
write#1, "1","2","3","4","5"
Close#1

'Read CSV File
open "c:\test.csv" for input as #1
input#1, A$,B$,C$,D$,E$
input#1, A$,B$,C$,D$,E$
input#1, A$,B$,C$,D$,E$
Close#1

Works perfect and real easy. I can also open up the file in excel.

In VB.NET I can write the file the same way with:

FileOpen(1, "c:\testfile2.csv", OpenMode.Output)
WriteLine(1, "1", "2", "3", "4")
WriteLine(1, "2", "2", "3", "4")
WriteLine(1, "3", "2", "3", "4")
FileClose(1)

Now, how can I easily read the file from VB.NET into variables???? It
is driving me crazy and there seems to be about 10 different ways but
I want a simple CSV read routine. THANKS for any help.

Sheila

Sep 5 '07 #3
"Sambantham Kuppusamy" <sa*********@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
vb.net is high Tech.
Is that sarcasm or an interesting variation on Ad Hominem (Ad Techinem?) ?

:^)

There's got to be a simpler way. What about "stream" reading. Also, people
have been writing header analysis routines for decades - I thought by now
header analysis would be implicit in any table reading
method/function/statement in a modern programming language...?

--
Timothy Casey GPEMC! >11950 is the nu****@fieldcraft.com.au 2email
Terms & conditions apply. See www.fieldcraft.biz/GPEMC
www.fieldcraft.biz & www.speed-reading-comprehension.com
www.geologist-1011.com & www.web-design-1011.com
Sep 5 '07 #4
Did you try it with AdoNet, that is a simple method and less clumsy (read
easily giving errors) as reading it as a kind of text file.

http://www.vb-tips.com/CSVDataSet.aspx

Cor
<ne**********@gmail.comschreef in bericht
news:11**********************@o80g2000hse.googlegr oups.com...
>I think VB.NET drives some people crazy because some simple VB6 things
seem so hard.

Here is some VB6 code:

'Write CSV File
open "c:\test.csv" for output as #1
write#1, "1","2","3","4","5"
write#1, "1","2","3","4","5"
write#1, "1","2","3","4","5"
Close#1

'Read CSV File
open "c:\test.csv" for input as #1
input#1, A$,B$,C$,D$,E$
input#1, A$,B$,C$,D$,E$
input#1, A$,B$,C$,D$,E$
Close#1

Works perfect and real easy. I can also open up the file in excel.

In VB.NET I can write the file the same way with:

FileOpen(1, "c:\testfile2.csv", OpenMode.Output)
WriteLine(1, "1", "2", "3", "4")
WriteLine(1, "2", "2", "3", "4")
WriteLine(1, "3", "2", "3", "4")
FileClose(1)

Now, how can I easily read the file from VB.NET into variables???? It
is driving me crazy and there seems to be about 10 different ways but
I want a simple CSV read routine. THANKS for any help.

Sheila
Sep 5 '07 #5
Hi,
I use this simple and convenient class.
============================================
Imports System.Collections.Generic
Imports System.IO

Public Class CSVParser

#Region "Local Variables"
Protected m_fields As New List(Of String)
Protected m_separator As String = String.Empty
Protected m_stream As System.IO.StreamReader = Nothing
Protected m_names As New Dictionary(Of String, Int32)
#End Region
#Region "Ctors"
Public Sub New(ByVal PathName As String, Optional ByVal
Separator As String = ",")
m_separator = Separator
If System.IO.File.Exists(PathName) Then
m_stream = New System.IO.StreamReader(PathName)
Else
Throw New Exception("File not found")
End If
End Sub
#End Region

#Region "Methods"
Public Sub Close()
m_stream.Close()
m_stream = Nothing
End Sub

Public Sub ResetNames()
m_names.Clear()
End Sub
' associate field with name
Public Sub SetName(ByVal index As Int32, ByVal FieldName As
String)
If FieldName.Length 0 Then
If m_names.ContainsKey(FieldName) Then
Throw New Exception("Duplicate Name " & FieldName)
Else
m_names.Add(FieldName, index)
End If
Else
Throw New Exception("Empty Name Supplied")
End If
End Sub
Public Function ReadLine() As Boolean
m_fields.Clear()
If m_stream IsNot Nothing AndAlso Not m_stream.EndOfStream
Then
Dim buffer As String = m_stream.ReadLine()
If buffer.Length 0 Then
Parse(buffer)
Return True
End If
End If
Return False
End Function
Protected Sub Parse(ByVal buffer As String)
m_fields.AddRange(buffer.Split(New String() {m_separator},
StringSplitOptions.None))
End Sub
Public Function Count() As Int32
Return m_fields.Count
End Function
' no out of range exceptions
Public Overloads Function GetValue(ByVal index As Int32) As
String
If index >= 0 AndAlso index < m_fields.Count Then
Return m_fields(index)
End If
Return String.Empty
End Function

Public Overloads Function GetValue(ByVal name As String) As
String
Dim index As Int32
If m_names.TryGetValue(name, index) Then
Return GetValue(index)
End If
Throw New Exception("Invalid Field Name " & name.ToUpper)
End Function
#End Region

End Class

Public Class CSVFNParser : Inherits CSVParser
#Region "Ctors"
Public Sub New(ByVal PathName As String, Optional ByVal
Separator As String = ",")
MyBase.New(PathName, Separator)
Initialize()
End Sub

Protected Sub Initialize()
If ReadLine() Then
Dim N As Int32 = 0
For Each s As String In m_fields
Dim FieldName As String = s.ToUpper
If m_names.ContainsKey(FieldName) Then
Throw New Exception("Duplicate Name " &
FieldName)
Else
m_names.Add(FieldName, N)
End If
N += 1
Next
End If
If m_names.Count = 0 Then
Throw New Exception("No Field Names in CSV")
End If
End Sub
#End Region

End Class

----------------------------------

And sample program:

Sub Main()

Dim csv As New CSVParser("C:\blablabla.csv", ",")
While csv.ReadLine()
Dim N As Int32 = csv.Count
Console.WriteLine("There are {0} fields in this line", N)
For j as Int32 = 0 To N-1
If csv.GetValue(j).Length 0 Then
Console.WriteLine("[{0}]", csv.GetValue(j))
End If
Next

Console.WriteLine("---------------------------------------------------")
End While
Console.ReadLine()
End Sub

HTH
Serge
http://www.sergejusz.com

Sep 5 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Batista, Facundo | last post by:
People: I'm trying to convert my father from using COBOL to Python, :) One difficult thing we stuck into is how to read, from python, files written with COBOL. Do you know a module that...
7
by: War Eagle | last post by:
I have two byte arrays and a char (the letter S) I was to concatenate to one byte array. Here is what code I have. I basically want to send this in a one buffer (byte array?) through a socket. ...
1
by: oni | last post by:
hi all, the program should be able to log on to a pop mail account and need to just read its headers and display. any help in this regard highly apperecialted. code pref VB.NET thankz in...
3
by: jamal | last post by:
HI guys I want to make a program that will read my external pop3 e-mail account and save them to an access/sql file on my workstation. what you guys think ? Can I make something like this with...
6
by: cdecarlo | last post by:
Hello, I've often found that I am writing little scripts at the interpretor to read a text file, perform some conversion, and then write the converted data back out to a file. I normally...
24
by: Michael | last post by:
Hi, I am trying to pass a function an array of strings, but I am having trouble getting the indexing to index the strings rather than the individual characters of one of the strings. I have...
2
by: Kevin Ar18 | last post by:
I posted this on the forum, but nobody seems to know the solution: http://python-forum.org/py/viewtopic.php?t=5230 I have a zip file that is several GB in size, and one of the files inside of it...
6
by: arnuld | last post by:
This works fine, I welcome any views/advices/coding-practices :) /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a program to store each line from a file into a *...
0
by: martinmercy2001 | last post by:
Could any body help me with creating a ring buffer class using a string. use memory circular buffer not an IO buffer. just read, write and seek method. Read method should take anumber and return the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.