Thread: Another question about file I/O, bytes and the like

  1. #1
    Registered User
    Join Date
    May 2002

    Post Another question about file I/O, bytes and the like

    Hi all, I lloked through your previous messages as best I could, but have been unable to work out this coding problem.

    The final goal of this program is to simply read some information from inside an image header file. We will know where the info is stored (in the exaple, the LOC) and how many bytes the information uses (in the example NUM_BYTES).

    In the current example, the file "cl" has a number stored at Byte #5. It is supposed to be 1 byte in size (field format = unsigned byte. This means it is 1 byte correct?) and and be between 0 and 6 (inclusive).

    The problem I am haveing is if I set LOC to 5 and NUM_BYTES to 1 (as they should be) I get an output of "4280320". If I use a NUM_BYTES of 3 I get an output of "6" which is reasonable, but why do I need to put a 3 here when the size is supposed to be 1? Or is this just a happenstance that if I put a 3 I like the results.

    Some more info...
    This program is running in Windows, the file was produced on the MAC. Is there some bit swapping I need to do (I dunno, Big Endian vs Little Endian or some such)?

    #include <fstream>
    #include <iostream>
    #include <stdlib.h>

    #define NUM_BYTES 1 //size of the data 2 b extracted (in bytes)
    #define LOC 5 //location of the data 2 b extracted (byte #)

    using namespace std;

    int main()
    { int iVal1;
    ifstream fin("cl", ios::binary);

    fin.seekg(LOC); ( (char *) &iVal1, NUM_BYTES);
    cout << endl << iVal1 << endl ;

    fin.close ();

    system ("pause");
    return 0;
    I had to use the (char *) &iVal1 in the 'cause if I just used iVal it gave me errors. If I tried to define char *iVal or char iVal the program wouldn't combile (but wouldn't give me any errors either ). If I din't use ios::binary (and the .read ()) then symbols would be returned (and I also didn't know how to fin >> 1 byte of data (or 4 bytes as needed for nutha part of program...)

    Sorry. That was really long winded. I would really appreciate your help tho.


  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Mmm, moved to C++ board

    > but why do I need to put a 3 here when the size is supposed to be 1?
    Your code is wrong

    > ( (char *) &iVal1, NUM_BYTES);
    Only modifies 1 byte of the 4 byte integer - the other 3 bytes are untouched.

    iVal = 0; ( (char *) &iVal1, NUM_BYTES);

    Should be better - and you only need to read 1 byte

    > Is there some bit swapping I need to do
    Not for single bytes
    For multi-byte values like int, the answer is perhaps

  3. #3
    Typical sizes for primitive types:

    type char = 1 byte
    type int = short = 4 bytes
    type long = 8 bytes
    type double = 16 bytes

    Sizes for type int varies from compiler to compiler though. Some 4, some 8, and some(?) 16.

  4. #4


    >>Mmm, moved to C++ board

    Oops, Musta Posted to wrong board, sorry.

    Thanks guys for the answers and explanantions. It helped a whole lot. The prog is working correctly AND I understand more about data types (and why my instructor always insisted I initialize variables first.)

    Thanks Again

Popular pages Recent additions subscribe to a feed