Results 1 to 8 of 8

Thread: Am not able to properly read a Text file

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2003
    Posts
    12

    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

  2. #2
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    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

  3. #3
    Lively Member
    Join Date
    Jan 2003
    Posts
    71
    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.

  4. #4
    Fanatic Member rudvs2's Avatar
    Join Date
    Mar 2001
    Location
    NZ
    Posts
    935
    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

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2003
    Posts
    12
    I havn't tried that.

    Can You Please post a sample code for the example i have stated above.

  6. #6
    Fanatic Member rudvs2's Avatar
    Join Date
    Mar 2001
    Location
    NZ
    Posts
    935
    sure here you go

    VB Code:
    1. Private Function makeDataSet() As DataSet
    2.         Dim Ds As New DataSet("TheData")
    3.         Dim table As New DataTable("TheTable")
    4.         Dim col As New DataColumn("TheNumbers")
    5.  
    6.         table.Columns.Add(col)
    7.         Dim theData As Integer = 0
    8.         Dim row As DataRow
    9.         For theData = 0 To 10
    10.             row = table.NewRow
    11.             row.Item("TheNumbers") = theData
    12.             table.Rows.Add(row)
    13.         Next
    14.  
    15.  
    16.         Ds.Tables.Add(table)
    17.         Return Ds
    18.     End Function
    19.  
    20.     Private Sub saveDataset(ByVal filename As String, ByVal Ds As DataSet)
    21.         Ds.WriteXml(filename)
    22.     End Sub
    23.  
    24.     Private Function LoadDataSet(ByVal filename As String) As DataSet
    25.         Dim Ds As DataSet
    26.         Ds.ReadXml(filename)
    27.         Return Ds
    28.     End Function

  7. #7
    Fanatic Member rudvs2's Avatar
    Join Date
    Mar 2001
    Location
    NZ
    Posts
    935
    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

  8. #8

    Thread Starter
    New Member
    Join Date
    Jan 2003
    Posts
    12
    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
  •  



Click Here to Expand Forum to Full Width