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

This is a discussion on Another question about file I/O, bytes and the like within the C++ Programming forums, part of the General Programming Boards category; Hi all, I lloked through your previous messages as best I could, but have been unable to work out this ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    3

    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)?


    -----------------CODE----------------------------
    #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);
    fin.read ( (char *) &iVal1, NUM_BYTES);
    cout << endl << iVal1 << endl ;

    fin.close ();

    system ("pause");
    return 0;
    }
    --------------/CODE------------------------
    I had to use the (char *) &iVal1 in the fin.read '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.

    Steve

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,665
    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

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

    iVal = 0;
    fin.read ( (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
    Unregistered
    Guest
    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
    StevenjLuke
    Guest

    Smile

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21