Click here to Skip to main content
15,921,697 members
Home / Discussions / C#
   

C#

 
GeneralRe: Windows Service in C#.Net 2.0, moving files from directory Pin
gus_br1-Mar-07 1:17
gus_br1-Mar-07 1:17 
QuestionHow to diplay web page inside another web page window? Pin
cebyrjoe228-Feb-07 9:27
cebyrjoe228-Feb-07 9:27 
AnswerRe: How to diplay web page inside another web page window? Pin
Wayne Phipps28-Feb-07 9:46
Wayne Phipps28-Feb-07 9:46 
AnswerRe: How to diplay web page inside another web page window? Pin
Vasudevan Deepak Kumar28-Feb-07 14:38
Vasudevan Deepak Kumar28-Feb-07 14:38 
QuestionHow to retrieve non fixed-length records from a binary file Pin
htres28-Feb-07 9:20
htres28-Feb-07 9:20 
AnswerRe: How to retrieve non fixed-length records from a binary file Pin
Luc Pattyn28-Feb-07 10:14
sitebuilderLuc Pattyn28-Feb-07 10:14 
GeneralRe: How to retrieve non fixed-length records from a binary file Pin
htres1-Mar-07 3:04
htres1-Mar-07 3:04 
GeneralRe: How to retrieve non fixed-length records from a binary file Pin
Luc Pattyn1-Mar-07 7:07
sitebuilderLuc Pattyn1-Mar-07 7:07 
Hi,

since you dont control the file format, here are the fundamentals you will need, plus
some suggestions:
- use one FileStream for your file
- use BinaryReader.ReadBytes() to read a number of bytes at the current position (it will
  advance the current position); problem here is you must specify the byte count
- create a number of classes or structs, one for each possible record type.
- if class/struct RecordType1 is one of the possible record types, you should give it
  two static methods:
bool Accept(FileStream) would read some bytes and decide whether or not the data fits the
record type for that class/struct; it should restore the filestream position as if
nothing happened (use FileStream.Position property to remember where you are in the file,
and to return to that position); it should not throw exceptions to the caller.
RecordType1 Load(FileStream) would read all the bytes needed to load a record of that type,
knowing it is of that type (since it will have been Accepted beforehand). Load does
advance the filestream, so it consumes the record and returns the result. It should throw
exceptions when something fails.
- details on Accept: you can try and recognize the first few bytes; JPEG always start
  with FF D8 and often with FF D8 FF E0; but nothing prevents other (non JPEG) records
  to also start with FF D8 !! So your collection of Accept() methods should be sufficiently
  accurate to discern the record types at hand.
- details on Load: you should know the byte count in order to read the right number
  of bytes; scanning for an end marker is difficult: even if JPEG always ends on FF D9,
  that does not mean the first FF D9 is the end of a JPEG (it could be a bit pattern
  in the middle of the pixel info).
- it is rather hard to decode JPEG, so I suggest to let GDI+ try and decode a JPEG image.
  One way would be to create a memory stream from your byte array, then call
  Image.FromStream(MemoryStream), but I suspect you could directly call
  Image.FromStream(FileStream) avoiding the byte count problem completely.
- you can create a new BinaryReader in every Accept and every Load method in each
  RecordType class/struct, or reuse a single one all over the place (dont try something
  intermediate).
- also provide a class/struct to handle the end-of-file record; it needs an Accept
  but does not need a Load() method.
- and now the finale: put all your Accept and Load methods in one loop to decode the
  entire file, as in:

try {
    FileStream fs=...
    for ( ; ; ) {   // each iteration reads one record, until end-of-file record is seen
        bool accepted=false;
        if (RecordType1.Accept(fs)) {
            accepted=true;
            RecordType1 rec1=RecordType1.Load(fs);
            ...do whatever needs to be done with rec1
            continue;
        }
       if (RecordType2.Accept(fs)) {
            accepted=true;
            RecordType2 rec2=RecordType2.Load(fs);
            ...do whatever needs to be done with rec2
            continue;
        }
        ...etc
        if (RecordTypeN.Accept(fs)) {
            ...this one recognizes end of file
            ...close the shared BinaryReader if there is one, close the FileStream
            break;
        }
       if (!accepted) throw new ApplicationException("Cant recognize record...");
    }
} catch(Exception exc) {
    ... handle the exception
}

- the order of the RecordType# blocks may be relevant, since the first one that
  returns a true on Accept will (have to) consume the data, so put the most strict
  acceptors first.


Basically thats it, the rest are details...

Smile | :)



Luc Pattyn

[My Articles]

AnswerRe: How to retrieve non fixed-length records from a binary file Pin
htres5-Mar-07 4:48
htres5-Mar-07 4:48 
GeneralRe: How to retrieve non fixed-length records from a binary file Pin
Luc Pattyn5-Mar-07 11:14
sitebuilderLuc Pattyn5-Mar-07 11:14 
GeneralRe: How to retrieve non fixed-length records from a binary file Pin
htres5-Mar-07 13:11
htres5-Mar-07 13:11 
AnswerRe: How to retrieve non fixed-length records from a binary file Pin
DavidAtAscent28-Feb-07 14:12
DavidAtAscent28-Feb-07 14:12 
QuestionHow to add new control instances with sprcified name from within a custom control at design time Pin
Galib Anwar28-Feb-07 9:08
Galib Anwar28-Feb-07 9:08 
QuestionRegistering a COM component to use in VB6 Pin
gus_br28-Feb-07 8:31
gus_br28-Feb-07 8:31 
AnswerRe: Registering a COM component to use in VB6 Pin
mike montagne28-Feb-07 8:43
mike montagne28-Feb-07 8:43 
AnswerRe: Registering a COM component to use in VB6 Pin
DavidAtAscent28-Feb-07 13:49
DavidAtAscent28-Feb-07 13:49 
QuestionDataGridView DataBinding problem Pin
royk12328-Feb-07 8:11
royk12328-Feb-07 8:11 
QuestionHow to Fix the Column Width? Pin
Khoramdin28-Feb-07 8:03
Khoramdin28-Feb-07 8:03 
AnswerRe: How to Fix the Column Width? Pin
Ed.Poore28-Feb-07 8:18
Ed.Poore28-Feb-07 8:18 
Questionc# installer - exception throws - space inbetween folder name Pin
jtamil2001@yahoo.com28-Feb-07 7:52
jtamil2001@yahoo.com28-Feb-07 7:52 
AnswerRe: c# installer - exception throws - space inbetween folder name Pin
Stefan Troschuetz28-Feb-07 7:57
Stefan Troschuetz28-Feb-07 7:57 
GeneralRe: c# installer - exception throws - space inbetween folder name Pin
jtamil2001@yahoo.com1-Mar-07 0:38
jtamil2001@yahoo.com1-Mar-07 0:38 
GeneralRe: c# installer - exception throws - space inbetween folder name Pin
Stefan Troschuetz1-Mar-07 0:57
Stefan Troschuetz1-Mar-07 0:57 
GeneralRe: c# installer - exception throws - space inbetween folder name Pin
jtamil2001@yahoo.com1-Mar-07 4:35
jtamil2001@yahoo.com1-Mar-07 4:35 
GeneralRe: c# installer - exception throws - space inbetween folder name Pin
Stefan Troschuetz1-Mar-07 4:45
Stefan Troschuetz1-Mar-07 4:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.