Writing integer to binary file using C++?

前端 未结 1 1038
春和景丽
春和景丽 2020-12-10 02:44

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

1条回答
  •  有刺的猬
    2020-12-10 02:53

    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(&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).

    0 讨论(0)
提交回复
热议问题