Read Dat File?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • iam_clint
    Recognized Expert Top Contributor
    • Jul 2006
    • 1207

    Read Dat File?

    Hello everyone I'm still learning vb.net and I'm stuck on something.
    Code:
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            Dim fInfo As New FileInfo("servercache.dat")
            Dim numBytes As Long = fInfo.Length
            Dim fStream As New FileStream("servercache.dat", FileMode.Open, FileAccess.Read)
            Dim br As New BinaryReader(fStream)
            Dim data As Char() = br.ReadChars(CInt(numBytes))
            'Dim data As Byte() = br.ReadBytes(CInt(numBytes))
            Dim test As String = ""
            For i = 0 To 5000
                test += Convert.ToString(data(i))
            Next
            MsgBox(test)
    
            br.Close()
            fStream.Close()
        End Sub
    This is how I attempted it but i keep just getting one little square...
    I attached the dat file zipped up, its from a game called call of duty 4 and its a server list cache. Any ideas or direction would be very helpful thanks.
    Attached Files
  • phvfl
    Recognized Expert New Member
    • Aug 2007
    • 173

    #2
    When reading file content I tend to use the following:

    Code:
    Dim contents As New StringBuilder()
    Dim enc As Encoding = New UTF8Encoding()
    
    Using fStream As FileStream = New FileStream("servercache.dat", FileMode.Open, FileAccess.Read)
    
          ' The maximum bytes to read.
          Dim toRead As Int32 = 1024 ' 1Kb
    
          ' The buffer for read bytes.
          Dim buffer(toRead - 1) As Byte
    
          ' The number of bytes that have been read.
          Dim read As Int32 = fStream.Read(buffer, 0, toRead)
    
          ' While there is any data that has been read
          Do While read > 0
    
            ' Get the string from the byte array.
            contents.Append(enc.GetString(buffer, 0, read))
    
            ' Read next batch of data into the buffer.
            read = fStream.Read(buffer, 0, toRead)
          Loop
    
          fStream.Close()
    End Using
    This code assumes an import of the System.Text namespace. The encoding used will be dependant upon the encoding of the file. The instantiation of the enc object could also have been written as:
    Code:
    Dim enc As Encoding = Encoding.UTF8
    A couple of items of note are the Using block and the StringBuilder.

    The Using block causes the Dispose method of an iDisposable object to be called once that item is out of scope.

    The StringBuilder is used instead of a string due to the iterative process of copying the contents of the file. A string is immutable so every time a concatanation is made a new instance of the object is created and the existing instance recreated. A StringBuilder is mutable and so should give better performance, particularly with larger files. To retrieve the string use the ToString() method of the StringBuilder.

    Comment

    • iam_clint
      Recognized Expert Top Contributor
      • Jul 2006
      • 1207

      #3
      Originally posted by phvfl
      When reading file content I tend to use the following:

      Code:
      Dim contents As New StringBuilder()
      Dim enc As Encoding = New UTF8Encoding()
       
      Using fStream As FileStream = New FileStream("servercache.dat", FileMode.Open, FileAccess.Read)
       
            ' The maximum bytes to read.
            Dim toRead As Int32 = 1024 ' 1Kb
       
            ' The buffer for read bytes.
            Dim buffer(toRead - 1) As Byte
       
            ' The number of bytes that have been read.
            Dim read As Int32 = fStream.Read(buffer, 0, toRead)
       
            ' While there is any data that has been read
            Do While read > 0
       
              ' Get the string from the byte array.
              contents.Append(enc.GetString(buffer, 0, read))
       
              ' Read next batch of data into the buffer.
              read = fStream.Read(buffer, 0, toRead)
            Loop
       
            fStream.Close()
      End Using
      This code assumes an import of the System.Text namespace. The encoding used will be dependant upon the encoding of the file. The instantiation of the enc object could also have been written as:
      Code:
      Dim enc As Encoding = Encoding.UTF8
      A couple of items of note are the Using block and the StringBuilder.

      The Using block causes the Dispose method of an iDisposable object to be called once that item is out of scope.

      The StringBuilder is used instead of a string due to the iterative process of copying the contents of the file. A string is immutable so every time a concatanation is made a new instance of the object is created and the existing instance recreated. A StringBuilder is mutable and so should give better performance, particularly with larger files. To retrieve the string use the ToString() method of the StringBuilder.

      Thanks for the help.. When I get this built i'll post the resulting code because i can't find anything on parsing this file on google.

      Comment

      Working...