473,657 Members | 2,521 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Read Dat File?

iam_clint
1,208 Recognized Expert Top Contributor
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, 554 views)
Dec 24 '08 #1
2 10388
phvfl
173 Recognized Expert New Member
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 Recognized Expert Top Contributor
@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
5317
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
2274
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 supposed to change the buffer size and see how this affects the read time. In other words, the buffer is supposed to limit how much of the file gets read per call, and cause some change in speed. I am supposed to do the same with fread as well, but...
1
6739
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 CLogHandler() { this.m_fsLog = new FileStream(strTodaysLogFile, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.Read); this.m_swLog = new StreamWriter(this.m_fsLog);
4
2733
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 Main()
8
23899
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
3300
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 namespace std; int read(string filename, int last_pos) {
0
5778
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 17640 every times., and i set byte read=new byte, i have the wav format paper, but i don't know how to read the data correctly into the byte array, and used for doing DFT.... here's my code, and i am little hurry...thanks for the helps first~
6
32881
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). Is there some way to burst read the whole file and later "extract" each line? Thanks in advance, Thomas Kowalski
9
7381
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 to read was maindata.xml . I cannot however seem to use the gzip module correctly. Have tried the program 2 ways for no success, any ideas would be appreciated. Attempt 1 #!/usr/bin/python
2
5446
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 is several GB in size. When it comes time to read the 5+GB file from inside the zip file, it fails with the following error: File "...\zipfile.py", line 491, in read bytes = self.fp.read(zinfo.compress_size) OverflowError: long it too large to...
0
8305
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8823
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8726
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8603
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7320
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6163
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1944
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1604
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.