1 year ago

#297368

test-img

holdtheline

use of reinterpret_cast while reading value from binary file

Let's suppose head1 is an 2D array of float and I want to fill it with values read from the binary file file. After opening the file with

ifstream file ("myfile.bin", ios::in|ios::binary);

I tried two different ways:

  1. file.read( reinterpret_cast<char*>(&head1), sizeof(head1) );
  2. file.read( (char*) &head1, sizeof(head1) );

Both solutions work, but what's exactly the difference between them? which is "safer"? After reading some documentation, for me the role of reinterpret_cast is still a bit obscure and from "Programming: Principles and Practice Using C++" I also read that is better to use this function only if necessary especially for portability problem. Thanks

c++

reinterpret-cast

0 Answers

Your Answer

Accepted video resources