Byte-Alignment...

SephirothSephiroth Fayetteville, NC, USA
OK, I need to read in a file written in DOS. This file is 16bit or whatever the old DOS alignment was. How do I make my structures or variables set to properly read in these files?

-[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]

Comments

  • : OK, I need to read in a file written in DOS. This file is 16bit or whatever the old DOS alignment was. How do I make my structures or variables set to properly read in these files?
    :
    : -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]
    :
    [blue]You have to find out the format of these DOS files - I mean the data layout. Also, I never heard of 16-bit file alignment... what is it? Any DOS file can be loaded as an array of bytes and processed according to a file format.[/blue]
  • SephirothSephiroth Fayetteville, NC, USA
    : [blue]You have to find out the format of these DOS files - I mean the data layout. Also, I never heard of 16-bit file alignment... what is it? Any DOS file can be loaded as an array of bytes and processed according to a file format.[/blue]
    :
    I mis-typed what I need. These files are byte-aligned. They are huge files containing all kinds of data and even directories of multiple types. I know what they're all about, but I need to figure out how I should read a byte-aligned file into an array. It was suggested that I somehow use a pragma declaration before and after my struct declarations, but I am not sure how to.

    -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]

  • SephirothSephiroth Fayetteville, NC, USA
    Just thought of how to do this and use 32bit variables! Correct me if I am wrong, but isn't a 16bit byte the same size as two 32bit bytes? If this is the case then check this loading method:
    [code]
    //Let's read in 4 bytes that were
    //written to the file under 16bit DOS
    fread(&(Array.Member), sizeof(char[8]), 1, DOSFile);
    [/code]
    Now would I have the data in my variable, or would it over-run into another variable?

    -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]

  • : Just thought of how to do this and use 32bit variables! Correct me if I am wrong, but isn't a 16bit byte the same size as two 32bit bytes? If this is the case then check this loading method:
    : [code]
    : //Let's read in 4 bytes that were
    : //written to the file under 16bit DOS
    : fread(&(Array.Member), sizeof(char[8]), 1, DOSFile);
    : [/code]
    : Now would I have the data in my variable, or would it over-run into another variable?
    :
    : -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]
    :
    :
    [blue]That will work. Except 'char[8]' usually means 8 bytes (I hope very strict coders will not be offended) and not 4. In your case you can do that:
    [code]
    fread(&(Array.Member), sizeof(short), 2, DOSFile);
    [/code]But, I doubt that it is exactly what you want. I think you want to read 2 bytes which was stored by DOS code and STUFF it into 4 bytes of some 'int' from modern compiler (which takes 4 bytes (usually...)). If that's the case - you have to do conversion - only reading will not do a trick, so you have to build the structures as they are in a DOS file, like so:
    [code]
    #pragma pack(push,1)
    typedef struct
    {
    char field1 [8];
    short iValue1; // 'short' is 2 bytes
    short iValue2;
    short iValue3;
    long lValue32;
    }
    DOS_FILE_TAG1;
    #pragma pack(pop)

    typedef struct
    {
    char field1 [8];
    int iValue1;
    int iValue2;
    int iValue3;
    long lValue32;
    }
    WIN32_FILE_TAG1;

    DOS_FILE_TAG1 tag1;
    WIN32_FILE_TAG1 tag1_now;

    fread(&tag1, sizeof(tag1), 1, DOSFile);

    // And then physically move all data into a new structure:
    strcpy (tag1_now.field1, tag1.field1);
    tag1_now.iValue1 = tag1.iValue1;
    tag1_now.iValue2 = tag1.iValue2;
    tag1_now.iValue3 = tag1.iValue3;
    tag1_now.lValue32 = tag1.lValue32;
    [/code]
    [/blue]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories