473,857 Members | 1,786 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# FileStream and BinaryReader Issue

6 New Member
Hi,

I'm currently making an application which reads a file and extracts information from it. Whole files are embedded in this single file, and information about each file that is embedded is stored at a specific location within this file.

Expand|Select|Wrap|Line Numbers
  1. fs = new FileStream(@"File Location", FileMode.Open);
  2. br = new BinaryReader(fs);
  3. rtbOutput.AppendText("English Title: " + getLanguage(br));
Expand|Select|Wrap|Line Numbers
  1. public string getLanguage(BinaryReader reader)
  2.         {
  3.             byte[] data = ReadSection(reader, 1296, 256); // reader variable, offset, length
  4.             return GetString(data);
  5.         }
Expand|Select|Wrap|Line Numbers
  1. public static byte[] ReadSection(BinaryReader reader, int offset, int length)
  2.         {
  3.             reader.BaseStream.Seek(offset, SeekOrigin.Begin);
  4.             return reader.ReadBytes(length);
  5.         }
That is the code that I am using to attempt to read the language from the file, however, I cannot seek past (what seems) 256 bytes; nothing else seems to be available in the stream. The file itself is around 3.1mb, so I know that there is more that should be available to seek to, and I don't want to load the whole file in to a buffer (that would take ages, VB uses FileGet and doesnt need to load the whole thing in to a buffer).

I would basically like to know how I seek past 256 bytes in a large file, without resorting to cycling through all the data and sticking it in a buffer. I've spent ages trying to think of a way to do this, and searching online to attempt to find a solution. I suppose what I'm looking for is somthing that lets me randomly access anywhere in a large file.

Apologies for in-efficient code and such, I'm reasonably new to C# :P
Mar 12 '07 #1
9 5515
Drayshak
6 New Member
Sorry, I forgot to add that I am using Visual Studio 2005, on Windows XP SP2 (im not sure if that makes much difference, but I thought I'd post it anyway :)
Mar 12 '07 #2
kenobewan
4,871 Recognized Expert Specialist
Have you tried calling reader.DiscardB ufferedData() after you perform the Seek() operation? This is so the stream reader does not get stale data from its buffer.
Mar 13 '07 #3
Drayshak
6 New Member
Have you tried calling reader.DiscardB ufferedData() after you perform the Seek() operation? This is so the stream reader does not get stale data from its buffer.
Thanks for the swift reply, I'll try this as soon as I get home :)
Mar 13 '07 #4
Drayshak
6 New Member
Have you tried calling reader.DiscardB ufferedData() after you perform the Seek() operation? This is so the stream reader does not get stale data from its buffer.
There doesn't seem to be a function called that, and I've tried the Flush() function as well, still no luck. I am still only getting the first 256 bytes of the file, and I cannot seek past that point.
Mar 13 '07 #5
kenobewan
4,871 Recognized Expert Specialist
Does it have anything to do with the reader length in:
Expand|Select|Wrap|Line Numbers
  1. byte[] data = ReadSection(reader, 1296, 256);
Mar 13 '07 #6
Drayshak
6 New Member
I thought the offset (1296 or something like that) would move the reader position forwards before reading the 256 bytes?

The 256 bytes is just a null padded 'universal title' for the file.
Mar 14 '07 #7
kenobewan
4,871 Recognized Expert Specialist
If it is not the reader length, then I don't know. Anyone else?
Mar 14 '07 #8
Drayshak
6 New Member
I've sorted the problem out. I have chosen to use the SetFilePointer function in C++ to do this instead.
Mar 14 '07 #9
kenobewan
4,871 Recognized Expert Specialist
Good work! Thanks for sharing the solution :).
Mar 15 '07 #10

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

Similar topics

2
391
by: Kai Bohli | last post by:
Hi all ! This is my first day with Visual Studio and C#, and I'm trying to send "raw data" to the printer port. I found the SendFileToPrinter example below in a knowledge base at MS site It works, but I need to replace the filestream with a memorystream cause there's no need to write files and then load them again just to print. So I rewrote the procedure and the call. The problem is of course that it dosen't work. If I replace the...
29
485
by: | last post by:
Hi, I am trying to optimize the reading of a huge binary file into a byte... I am doing the following.. byte ba = new byte;
3
2284
by: James Dean | last post by:
I create a new filestream and then i move i use the readbyte function and the position function when required. These seem to be cripplingly slow for some reason. This readbyte maybe is the problem. Do i need to use a binaryReader with this in order for it to be faster?. I am manipulating a compressed image file. Because i am reading in so many bytes it makes it inefficient. Could somebody have a suggestion as how to remedy this problem.
4
11940
by: Paul Steele | last post by:
I am writing a client/server application that communicates over tcp. The code I use to initiate the connection is as follows: NetworkStream networkStream = new NetworkStream(ClientSocket); toClient = new BinaryWriter(networkStream); fromClient = new BinaryReader(networkStream); There is similar code on the client side. This set up works fine and communication does occur. However, the communication is very slow. This is especially...
5
7511
by: Tim | last post by:
Hi, I'm experiencing some problem with the following code: st = File.Open(sFilename, FileMode.Open, FileAccess.ReadWrite) br = New BinaryReader(st) Do Until br.PeekChar = -1 Dim buffer() As Byte = br.ReadBytes(1024) ...
0
1326
by: James Johnson | last post by:
I have a file that I open a FileStream and a StreamReader for. I issue a SEEK and read a series of records. I then issue another SEEK, no errors, but when I issue the read it picks up where the first read stopped. I have to close the FileStream and the StreamReader, reopen them and then issue the second SEEK. I tried issuing a FLUSH before the second SEEK but the results were the same.
3
2513
by: Hugh Janus | last post by:
Hi group, I am using a TCPStream together with a FileStream to send a file across a network. Everything works fine except for one thing. Always, at the end of the file there are several lines of white space. This is no problem for text files but for binaries it is making them corrupt. I only realised this when using checksums to verify that the copied file is the same. However, I cannot seem to work out where this additional data is...
2
2080
by: John | last post by:
I am trying to familiarize myself with filestreams, but I have run into the following issues. I am hoping that someone can shed some light on these issues for me please. I am using the following sample code to write (keeping it simple) private void button1_Click(object sender, EventArgs e) { String DATFILE = Directory.GetCurrentDirectory() + "\\Test.dat"; if (File.Exists(DATFILE))
1
2002
by: hzgt9b | last post by:
Using VS2005, VB.NET, I have a multi-threaded application. In one of my threads I need to read a file as a binary stream. I am able to accomplish this using the following code: 1 Dim BinaryFile As FileStream = New FileStream(<some file name>, FileMode.Open, FileAccess.Read, FileShare.Read) 2 Dim BinRead As BinaryReader = Nothing 3 Me.BinRead = New BinaryReader(BinaryFile) 4 Me.BinRead.BaseStream.Seek(0, SeekOrigin.Begin)
0
9923
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9768
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
11083
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
10394
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
9549
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
7940
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
4592
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
4191
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3215
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.