473,500 Members | 1,664 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 5495
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.DiscardBufferedData() 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.DiscardBufferedData() 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.DiscardBufferedData() 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...
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
2271
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....
4
11891
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);...
5
7470
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()...
0
1280
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...
3
2496
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...
2
2060
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...
1
1989
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...
0
7182
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,...
1
6906
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
7397
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...
0
5490
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,...
1
4923
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...
0
4611
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3110
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3106
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
316
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...

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.