473,395 Members | 1,689 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,395 software developers and data experts.

Read Dat File?

iam_clint
1,208 Expert 1GB
Hello everyone I'm still learning vb.net and I'm stuck on something.
Expand|Select|Wrap|Line Numbers
  1.     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
  2.         Dim fInfo As New FileInfo("servercache.dat")
  3.         Dim numBytes As Long = fInfo.Length
  4.         Dim fStream As New FileStream("servercache.dat", FileMode.Open, FileAccess.Read)
  5.         Dim br As New BinaryReader(fStream)
  6.         Dim data As Char() = br.ReadChars(CInt(numBytes))
  7.         'Dim data As Byte() = br.ReadBytes(CInt(numBytes))
  8.         Dim test As String = ""
  9.         For i = 0 To 5000
  10.             test += Convert.ToString(data(i))
  11.         Next
  12.         MsgBox(test)
  13.  
  14.         br.Close()
  15.         fStream.Close()
  16.     End Sub
  17.  
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
File Type: zip servercache.zip (250.5 KB, 553 views)
Dec 24 '08 #1
2 10362
phvfl
173 Expert 100+
When reading file content I tend to use the following:

Expand|Select|Wrap|Line Numbers
  1. Dim contents As New StringBuilder()
  2. Dim enc As Encoding = New UTF8Encoding()
  3.  
  4. Using fStream As FileStream = New FileStream("servercache.dat", FileMode.Open, FileAccess.Read)
  5.  
  6.       ' The maximum bytes to read.
  7.       Dim toRead As Int32 = 1024 ' 1Kb
  8.  
  9.       ' The buffer for read bytes.
  10.       Dim buffer(toRead - 1) As Byte
  11.  
  12.       ' The number of bytes that have been read.
  13.       Dim read As Int32 = fStream.Read(buffer, 0, toRead)
  14.  
  15.       ' While there is any data that has been read
  16.       Do While read > 0
  17.  
  18.         ' Get the string from the byte array.
  19.         contents.Append(enc.GetString(buffer, 0, read))
  20.  
  21.         ' Read next batch of data into the buffer.
  22.         read = fStream.Read(buffer, 0, toRead)
  23.       Loop
  24.  
  25.       fStream.Close()
  26. 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:
Expand|Select|Wrap|Line Numbers
  1. 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.
Dec 24 '08 #2
iam_clint
1,208 Expert 1GB
@phvfl

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.
Jan 28 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Yang Li Ke | last post by:
Hi guys! I have some datas that I must check everytime a visitor comes to my site What is better to do: 1- Read data from a file or 2- Read data from a mysql db Thank you
10
by: ZafT | last post by:
Thanks in advance for any tips that might get me going in the right direction. I am working on a simple exercise for school that is supposed to use read to read a file (about 10 MB). I am...
1
by: cnu | last post by:
My program generates a log file for every event that happens in the program. So, I open the file and keep it open till the end. This is how I open the file for writing: <CODE> public...
4
by: ESPN Lover | last post by:
Below is two snippets of code from MSDN showing how to read a file. Is one way preferred over the other and why? Thanks. using System; using System.IO; class Test { public static void...
8
by: a | last post by:
I have a struct to write to a file struct _structA{ long x; int y; float z; } struct _structA A; //file open write(fd,A,sizeof(_structA)); //file close
5
by: lovecreatesbea... | last post by:
The condition at line 31 is added to check if the program finished to read the whole file. Is it needed and correct? Thank you. #include <fstream> #include <iostream> #include <string> using...
0
by: lovecarole | last post by:
hi, i am the student who should write a program about reading wav file and do the DFT. actually i don't know how to read data of the wav song and save it into the array... if i want to read...
6
by: Thomas Kowalski | last post by:
Hi, currently I am reading a huge (about 10-100 MB) text-file line by line using fstreams and getline. I wonder whether there is a faster way to read a file line by line (with std::string line)....
9
by: flebber | last post by:
I was working at creating a simple program that would read the content of a playlist file( in this case *.k3b") and write it out . the compressed "*.k3b" file has two file and the one I was trying...
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.