C# FileStream and BinaryReader Issue

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Drayshak
    New Member
    • Mar 2007
    • 6

    C# FileStream and BinaryReader Issue

    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.

    Code:
    fs = new FileStream(@"File Location", FileMode.Open);
    br = new BinaryReader(fs);
    rtbOutput.AppendText("English Title: " + getLanguage(br));
    Code:
    public string getLanguage(BinaryReader reader)
            {
                byte[] data = ReadSection(reader, 1296, 256); // reader variable, offset, length
                return GetString(data);
            }
    Code:
    public static byte[] ReadSection(BinaryReader reader, int offset, int length)
            {
                reader.BaseStream.Seek(offset, SeekOrigin.Begin);
                return reader.ReadBytes(length);
            }
    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
  • Drayshak
    New Member
    • Mar 2007
    • 6

    #2
    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 :)

    Comment

    • kenobewan
      Recognized Expert Specialist
      • Dec 2006
      • 4871

      #3
      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.

      Comment

      • Drayshak
        New Member
        • Mar 2007
        • 6

        #4
        Originally posted by kenobewan
        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 :)

        Comment

        • Drayshak
          New Member
          • Mar 2007
          • 6

          #5
          Originally posted by kenobewan
          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.

          Comment

          • kenobewan
            Recognized Expert Specialist
            • Dec 2006
            • 4871

            #6
            Does it have anything to do with the reader length in:
            Code:
            byte[] data = ReadSection(reader, 1296, 256);

            Comment

            • Drayshak
              New Member
              • Mar 2007
              • 6

              #7
              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.

              Comment

              • kenobewan
                Recognized Expert Specialist
                • Dec 2006
                • 4871

                #8
                If it is not the reader length, then I don't know. Anyone else?

                Comment

                • Drayshak
                  New Member
                  • Mar 2007
                  • 6

                  #9
                  I've sorted the problem out. I have chosen to use the SetFilePointer function in C++ to do this instead.

                  Comment

                  • kenobewan
                    Recognized Expert Specialist
                    • Dec 2006
                    • 4871

                    #10
                    Good work! Thanks for sharing the solution :).

                    Comment

                    Working...