|
-
October 1st, 2006, 09:52 AM
#1
reading Comma delimited file strings inbetween commas
I have a comma delimited file containing a string
new parameter1, new parameter 2, new parameter 3
I want to read the string inbetween the commas.
In VB 6 the following code worked fine.
Code:
Public Sub fileread()
'Read parameters from PrintFile w/debug print
mpickedfilename = App.Path + "\Parameters.txt"
Open mpickedfilename For Input As #20
'get parameters count from printFile
mparametercount = 0
Do While Not EOF(20)
Input #20, mfileparameter 'count array for redim
mparametercount = mparametercount + 1
Loop
Close #20
ReDim mparameter(mparametercount)
'Get parameters form printFile
mpickedfilename = App.Path + "\Parameters.txt"
Open mpickedfilename For Input As #26
mparametercount = 0
Do While Not EOF(26)
Input #26, mparameter(mparametercount)
'MsgBox mparameter(mparametercount)
mparametercount = mparametercount + 1
Loop
Close #26
End Sub
But after a VB2005 conversion this code reads inbetween
spaces and not the commas. Is there solution
for this.
thank you,
Last edited by HanneSThEGreaT; October 2nd, 2006 at 04:45 AM.
Reason: Added Code Tags
-
October 1st, 2006, 10:20 AM
#2
Re: reading Comma delimited file strings inbetween commas
Code:
Dim str As StreamReader = New StreamReader(New FileStream("FiletoRead.csv", FileMode.Open))
Dim strline, arr(), x,y,z As String
Try
NextLine:
strline = str.ReadLine
arr = strline.Split(",")
x = arr(0)
y = arr(1)
z = arr(2)
GoTo NextLine
Catch
End Try
This code will read lines having three strings separated by commas into three variables x, y, and z. Good luck.
Last edited by HanneSThEGreaT; October 2nd, 2006 at 04:48 AM.
Reason: Added Code Tags
-
October 1st, 2006, 01:38 PM
#3
Re: reading Comma delimited file strings inbetween commas
Code:
'datatable to store the parameters separated by comma's
'into datatable columns
Dim dt As New DataTable
'3 columns into the datatable for 3 parameters
dt.Columns.Add("Col1", GetType(String))
dt.Columns.Add("Col2", GetType(String))
dt.Columns.Add("Col3", GetType(String))
Dim reader As New IO.StreamReader("C:\test1.csv")
'loop until end of file
While reader.Peek <> -1
Dim sFile As String = reader.ReadLine
'split the parameters
Dim str() As String = sFile.Split(",")
'get data from csv file into datatable datarow
Dim r As DataRow = dt.NewRow
r(0) = str(0)
r(1) = str(1)
r(2) = str(2)
dt.Rows.Add(r)
End While
'Show it in dataGrid or use the datatable
Me.DataGridView1.DataSource = dt
-
October 1st, 2006, 02:46 PM
#4
Re: reading Comma delimited file strings inbetween commas
If your values will never contain commas, then the above codes should work fine. You will come into problems if your values contain commas as well, because it would also split on the commas inside the values. Usually quotes are added for those values, something like ("Smith, John", 42, 32). There, if you split on the commas, then you will receive 4 values, with quotes inside of the values as well, when there should be 3 values, and no quotes.
You can check out my signature of a homebrew CSV and TAB file parser function that addresses those issues, or you can read the CSV file using ADO.NET, using a connection string for a text file, which you can get at http://www.connectionstrings.com
-
October 2nd, 2006, 04:35 AM
#5
Re: reading Comma delimited file strings inbetween commas
Hi, This is something I quickly whipped up to answer your question, the answer is a Webapplication format, (as that is what I had open at the time, ) but this should give you enough grounding on the concepts to further exapnd
Code:
Dim strDoc As System.IO.StreamReader = New System.IO.StreamReader(New System.IO.FileStream(Server.MapPath("somedocument.txt"), System.IO.FileMode.Open))
Dim strLine As String
Dim strArray() As String
Try
While strDoc.Peek <> -1
strArray = strDoc.ReadLine.Split(",")
For i As Integer = strArray.GetLowerBound(0) To strArray.GetUpperBound(0)
Response.Write(strArray(i) & "<br>")
Next
End While Catch ex As Exception
Response.Write(ex.Message)
Finally
strDoc.Close()
End Try
Last edited by cykophysh; October 3rd, 2006 at 09:18 AM.
Kind Regards,
Gary
www.threenineconsulting.com
"A fool will learn nothing from a wiseman , but a wiseman will learn plenty from a fool"
-
October 2nd, 2006, 04:51 AM
#6
Re: reading Comma delimited file strings inbetween commas
Never use the Goto Statement!!!
 Originally Posted by Gizmo001
Code:
Dim str As StreamReader = New StreamReader(New FileStream("FiletoRead.csv", FileMode.Open))
Dim strline, arr(), x,y,z As String
Try
NextLine:
strline = str.ReadLine
arr = strline.Split(",")
x = arr(0)
y = arr(1)
z = arr(2)
GoTo NextLine
Catch
End Try
This code will read lines having three strings separated by commas into three variables x, y, and z. Good luck.
Kind Regards,
Gary
www.threenineconsulting.com
"A fool will learn nothing from a wiseman , but a wiseman will learn plenty from a fool"
-
October 2nd, 2006, 05:26 AM
#7
Re: reading Comma delimited file strings inbetween commas
Gigemboy
Code:
Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\;Extended Properties=Text;"
Dim objConn As New OleDb.OleDbConnection(sConnectionString)
objConn.Open()
Dim objCmdSelect As New OleDb.OleDbCommand("SELECT * FROM test1.csv", objConn)
Dim objAdapter1 As New OleDb.OleDbDataAdapter()
objAdapter1.SelectCommand = objCmdSelect
objAdapter1.Fill(oDS)
DataGridView1.DataSource = oDS.Tables(0).DefaultView
objConn.Close()
-
October 2nd, 2006, 05:28 AM
#8
Re: reading Comma delimited file strings inbetween commas
also try this method
Code:
Dim oCon As Odbc.OdbcConnection
Dim oDA As Odbc.OdbcDataAdapter
Dim sConString As String
Dim sFolder As String = "C:\"
Dim sFile As String = "test1.csv"
sConString = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" & sFolder & ";"
oCon = New Odbc.OdbcConnection(sConString)
oDA = New Odbc.OdbcDataAdapter("Select * From [" + sFile + "]", oCon)
oDA.Fill(oDS)
DataGridView1.DataSource = oDS.Tables(0).DefaultView
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|