|
-
Mar 24th, 2003, 07:05 AM
#1
Thread Starter
New Member
Am not able to properly read a Text file
Hi,
I am able to open a text file, write in the opened text file, load the text file for reading, but am not able to figure out a proper of reading the values.
I have separated values by commas.
The values in the text file is as follows :
71, 72, 81, 72, 72, 81, 73, 72, 81, 75, 72, 81, 77, 72, 81,
I want to read as
A = 71
B = 72
C = 81
And then again
A = 72
B = 72
C = 81
And again ...
And
Also i am not sure whether am using the peek function correctly
The code is as below :-
----------------------------------------------------------------------------
'For writing the values in the text file ... this is working fine ...
TxtWrite = TxtFile.CreateText("C:\File.txt")
TxtWrite.Write(A)
TxtWrite.Write(",")
TxtWrite.Write(B)
TxtWrite.Write(",")
TxtWrite.Write(C)
TxtWrite.Write(",")
'Am not able to read it ...
TxtRead = TxtFile.OpenText(FNameTxt)
ReadAgain:
While TxtRead.Peek = ","
Dim A As Double
A = TxtRead.ReadToEnd
MsgBox(A)
End While
While TxtRead.Peek = ", "
Dim B As Double
B = TxtRead.ReadToEnd
MsgBox(B)
End While
While TxtRead.Peek = ", "
Dim C As Double
C = TxtRead.ReadToEnd
MsgBox(C)
End While
Goto ReadAgain
TxtRead.Close()
End Sub
----------------------------------------------------------------------------------
Thanx in Advance
-
Mar 24th, 2003, 11:37 AM
#2
Frenzied Member
you might want to re-format your text file and put 3 numbers on each line, or you can read all the numbers into an array, then get three numbers at a time to put into your variables.
Dont gain the world and lose your soul
-
Mar 24th, 2003, 01:48 PM
#3
Lively Member
I'm not sure I understand what you're trying to do, but the following code will read a text file and process the individual characters. Peek will look at the next character, but not remove it from the stream.
Private Sub ReadButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReadButton.Click
Dim filename As String
Dim getChar As Char
Dim buffer(1) As Char
Dim StreamReaderObj As System.IO.StreamReader
filename = "test.txt"
If System.IO.File.Exists(filename) Then
StreamReaderObj = System.IO.File.OpenText(filename)
Do Until StreamReaderObj.Peek = -1
StreamReaderObj.Read(buffer, 0, 1)
getChar = buffer(0)
Select Case getChar
Case ","
MsgBox("comma")
Case "1"
MsgBox("one")
End Select
Loop
StreamReaderObj.Close()
End If
End Sub
Last edited by ggprogram; Mar 24th, 2003 at 01:51 PM.
-
Mar 25th, 2003, 11:05 PM
#4
Fanatic Member
why use a text file at all?
Wouldnt it be easier to store the data in a dataset then just persist the dataset to xml when you want to save the data to file?
you easily read that xml back into a dataset if you need to reaccess it
-
Mar 25th, 2003, 11:09 PM
#5
Thread Starter
New Member
I havn't tried that.
Can You Please post a sample code for the example i have stated above.
-
Mar 25th, 2003, 11:18 PM
#6
Fanatic Member
sure here you go
VB Code:
Private Function makeDataSet() As DataSet
Dim Ds As New DataSet("TheData")
Dim table As New DataTable("TheTable")
Dim col As New DataColumn("TheNumbers")
table.Columns.Add(col)
Dim theData As Integer = 0
Dim row As DataRow
For theData = 0 To 10
row = table.NewRow
row.Item("TheNumbers") = theData
table.Rows.Add(row)
Next
Ds.Tables.Add(table)
Return Ds
End Function
Private Sub saveDataset(ByVal filename As String, ByVal Ds As DataSet)
Ds.WriteXml(filename)
End Sub
Private Function LoadDataSet(ByVal filename As String) As DataSet
Dim Ds As DataSet
Ds.ReadXml(filename)
Return Ds
End Function
-
Mar 25th, 2003, 11:22 PM
#7
Fanatic Member
also if your table design is not dynamic eg you will always have the same number of columns you can make your dataset creation even easier by designing a schema. Then you simply create the dataset object and read in the schema removing the need to write all the code to build the dataset.
you could then if you wanted to execute the xsd.exe tool to generate a strongly typed dataset which you then treat just like any normal object
-
Mar 25th, 2003, 11:27 PM
#8
Thread Starter
New Member
Thanx.
Will take some time to digest this concept.
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
|