19

I have a very simple question, which happens to be hard for me since this is the first time I tried working with binary files, and I don't quite understand them. All I want to do is write an integer to a binary file.

Here is how I did it:

#include <fstream>
using namespace std;
int main () {
    int num=162;
    ofstream file ("file.bin", ios::binary);
    file.write ((char *)&num, sizeof(num));
    file.close (); 
    return 0;
}

Could you please tell me if I did something wrong, and what?

The part that is giving me trouble is line with file.write, I don't understand it.

Thank you in advance.

jww
  • 97,681
  • 90
  • 411
  • 885
eqagunn
  • 221
  • 1
  • 4
  • 11
  • 1
    Does it work? How does it differ from what you expect? – Kerrek SB Feb 11 '12 at 22:29
  • 2
    Nitpick advice: 1) say `reinterpret_cast(&num)`, and omit the `file.close()` and the `return 0`, as all those happen automatically. – Kerrek SB Feb 11 '12 at 22:29
  • @KerrekSB Could you please explain what reinterpret_cast(&num) does? – eqagunn Feb 11 '12 at 22:32
  • 2
    It casts the type of `&num` to `const char *`, similar to your current cast, but it's a more deliberate way of writing what you intend than the crude C cast. It's just a matter of style and finesse, but you should get used to saying *precisely* what you want in C++. – Kerrek SB Feb 11 '12 at 22:33
  • Here is a [link explaining write](http://en.cppreference.com/w/cpp/io/basic_ostream/write). I recommend looking up the function signatures on that site when you don`t know what something does. – Jesse Good Feb 11 '12 at 22:37
  • You might find a hex editor useful for examining the file. There's a good free one here: http://www.chmaas.handshake.de/delphi/freeware/xvi32/xvi32.htm – Steve Wellens Feb 11 '12 at 23:13
  • @KerrekSB - he should *not* omit the return 0, as AFAIK only some compilers add it automatically and the standard does require it. – Asaf Feb 11 '12 at 23:42
  • @Asaf: "As AFAIK"?? What do you think "AFAIK" means? :-) But I can tell now that you love redundancy! – Kerrek SB Feb 12 '12 at 00:42
  • 2
    @KerrekSB: Yeah, yeah. That's what happens when you type without thinking. To my defense you can rephrase the sentence like that: "..., as *to the best of my knowledge* only some compilers...", and the marked phrase is synonym to 'as far as I know'. – Asaf Feb 12 '12 at 10:38

1 Answers1

36

The part that is giving me trouble is line with file.write, I don't understand it.

If you read the documentation of ofstream.write() method, you'll see that it requests two arguments:

  1. a pointer to a block of data with the content to be written;

  2. an integer value representing the size, in bytes, of this block.

This statement just gives these two pieces of information to ofstream.write():

file.write(reinterpret_cast<const char *>(&num), sizeof(num));

&num is the address of the block of data (in this case just an integer variable), sizeof(num) is the size of this block (e.g. 4 bytes on 32-bit platforms).

xorover
  • 94
  • 9
  • 3
    Important notice here: it writes data from the memory as it is stored there and resulting binary file will be not readable on machines with different endianness. – Mikolasan Dec 13 '19 at 04:19
  • @Mikolasan how can i make it so that the binary file is readable on different machines? – thatGuy Jun 16 '21 at 19:33
  • @thatGuy your code should check [what endianness](https://stackoverflow.com/a/38141476/1104612) on the running machine, if it differs from binary file format, then [convert](https://stackoverflow.com/q/105252/1104612) the data after reading or before writing, otherwise read/write and use as it is. – Mikolasan Jun 17 '21 at 16:53