1

I'm not sure if I'm doing this right so I want to check my code. it works but I'm not sure its working right. I need it to read the binary file, and store the 16 bit integers in an array of ints that is the exact size needed. I tried to do sizeof(storage[i]) so I could see if I was storing 16 bits but it says 32 (I'm guessing because int automatically allocates 4 bytes?

        void q1run(question q){
        int end;
        std::string input = q.programInput;
        std::ifstream inputFile (input.c_str(), ios::in | ios::binary);                     //Open File
        if(inputFile.good()){                                       //Make sure file is open before trying to work with it
                                                                    //Begin Working with information
           cout << "In File:  \t" << input << endl;
           inputFile.seekg(0,ios::end);
           end=inputFile.tellg();
           int numberOfInts=end/2;
           int storage[numberOfInts];
           inputFile.clear();
           inputFile.seekg(0);
           int test = 0;


           while(inputFile.tellg()!=end){       
               inputFile.read((char*)&storage[test], sizeof(2));
               cout << "Currently at position" << inputFile.tellg() << endl;
               test++;
           }

           for(int i=0;i<numberOfInts;i++){
               cout << storage[i] << endl;
           }
       }else{
           cout << "Could not open file!!!" << endl;
      }
 }

EDIT:::::::::::::::::::::::::::::::::::::::::::::;

I changed the read statement to:

      inputFile.read((char*)&storage[test], sizeof(2));

and the array to type short. now Its pretty well working except the output is a little strange:

      In File:        data02b.bin
      8
      Currently at position4
      Currently at position8
      10000
      10002
      10003
      0

I'm not sure what's in the .bin file, but I'm guessing the 0 shouldn't be there. lol

user3091834
  • 17
  • 1
  • 5

4 Answers4

9

Use: int16_t in <cstdint>. (guaranteed 16bits)

Shorts and ints can be of various sizes depending on the architecture.

ScarletAmaranth
  • 5,065
  • 2
  • 23
  • 34
5

Yes, int is 4 bytes (on 32-bit x86 platform).

You have two problems:

  1. As Alec Teal correctly mentioned in comment, you have your storage declared as int, which means 4 bytes. Not a problem, really - your data will fit.
  2. Actual problem: your line which is reading the file: inputFile.read((char*)&storage[test], sizeof(2)); is actually reading 4 bytes, because 2 is integer, so sizeof(2) is 4. You don't need sizeof there.
DarkWanderer
  • 8,739
  • 1
  • 25
  • 56
  • While ScarletAmaranth's comment was technically right it's annoyingly pedantic, we're fine assuming a "normal" arch right? I've not seen one for example where a word isn't a multiple of 8 bits. – Alec Teal Dec 11 '13 at 18:12
  • BTW upvote for spotting `sizeof(2)` - I completely ignored that - as did everyone else! – Alec Teal Dec 11 '13 at 18:13
  • @AlecTeal: thanks. It's the main problem anyways - storing shorts as ints is perfectly fine, the problem is that the file reading is messed up. – DarkWanderer Dec 11 '13 at 18:14
  • Exactly, I mentioned that because new programmers tend to focus on memory more than they need to (or indeed ought to), too often I see people turning off RTTI or forcing GCC to not pack stuff for cache-hits, awful :P – Alec Teal Dec 11 '13 at 18:15
  • I got it figured out based on this, had to use sizeof(1) or sizeof(short). thanks! – user3091834 Dec 11 '13 at 18:24
  • @DarkWanderer wtf at `sizeof(1)` - if you ever find out, tell me! – Alec Teal Dec 11 '13 at 18:27
  • @AlecTeal: What does "word isn't multiple of 8 bits" have to do with assuming that `sizeof(int)` == 4, or that `sizeof(short)` == 2? There are (I won't say plenty, but several) common architectures where `sizeof(int)` is not 4, even if `CHARBIT` is 8. I'm all for making assumptions when it is inconvenient to do otherwise, but in this case, we have `int16_t`, which is convenient, and makes the intent of the code more clear. – Benjamin Lindley Dec 11 '13 at 18:30
  • @BenjaminLindley totally, but the word pedant exists, the point of my comment was "has anyone ever actually seen a case where it's not a multiple of 8 bits to the word." - don't worry about it, the question is solved now. – Alec Teal Dec 11 '13 at 18:32
0

Storage is declared as an 'array' of "int"s, sizeof(int)=4

This shouldn't be a problem though, you can fit 16 bit values in 32 bit spaces, you probably meant short storage[...

Additionally for the sake of full-disclosure, sizes are defined in terms of sizeof(char) as a monotonically increasing sequence.

4 is by far the most common though, hence the assumption. (Limits.h will clarify)

Alec Teal
  • 5,770
  • 3
  • 23
  • 50
  • `sizeof(char)` is by definition 1. I don't see how you can define sizes of other types *in terms of sizeof(char)*. – Praetorian Dec 11 '13 at 20:18
  • @Praetorian well done? Anyway a a char is a number of 0s and 1s or "bits", and there's no reason a char must be a multiple of 8 bytes (usually just 8) it could be 7! limits.h defines these limits so you can create portable code. – Alec Teal Dec 11 '13 at 20:38
  • No, actually a `char` [must be at least 8 bits](http://stackoverflow.com/a/5516161/241631). I'm aware of *limits.h* and how to write portable code, since I've worked a lot on a TI DSP with 16-bit chars. But your response fails to address my original question, which was how sizes of other types can be *defined in terms of sizeof(char)* as you claim in your answer. Also, I don't see the need for the sarcasm in your reply. – Praetorian Dec 11 '13 at 20:56
0

One way to store 16 bits integers is to use the type short or unsigned short.

You used sizeof(2) which is equal to 4 because 2 is of type int so the the way to read 16 is to make storage of type short and the read:

short storage[numberOfInts];
....    
inputFile.read((char*)&storage[test], sizeof(short));

You can find here a table containing the sizes of all the types.

Raxvan
  • 6,257
  • 2
  • 25
  • 46
  • Thank you, I gave the check to the comment above me for saying almost the same thing, but very helpful nonetheless – user3091834 Dec 11 '13 at 18:25