static_cast
does not perform the unsafe conversion between pointers to unrelated types. That's the job of reinterpret_cast, which you would use when trying to write something that's not a
char or array of it.
The C-style cast tries very hard to coerce one type to another, even doing things that are often blatantly wrong (e.g. by silently removing cv-qualifiers), while the C++-style casts would fail to compile. Basically the C-style cast repeatedly applies "enhanced" C++-style casts until a sequence is found. For this reason, the C++-style casts are superior.
Array types may undergo an implicit array-to-pointer conversion. This means that an expression of array type can be converted to a pointer to the first element of the array. This doesn't happen here: the expression
&array_name
is a pointer to an array.
Where
array
is exactly an array of
char,
unsigned char,
signed char, or
std::byte, the following is correct:
file.write(array, sizeof(array));
Although that's awfully fragile, as its correctness depends on the type of
array
. In particular, for arrays of
T where
sizeof(T) != sizeof(char),
sizeof(array)
is
not the number of elements in the array. Further, it's common to mistake pointers for arrays: best practice suggests always passing a size explicitly.
To write and read, e.g., an array of int:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
#include <iostream>
#include <fstream>
int main() {
static constexpr int sz = 5;
std::fstream file("brainfart.bin", std::ios::in | std::ios::out |
std::ios::trunc);
// write out
int write_from[sz] = { 10, 20, 30, 40, 50 };
if (! file.write(reinterpret_cast<char const*>(write_from),
sz * sizeof *write_from))
{ std::cerr << "write failed\n"; return 1; }
// Seek to the beginning
file.seekg(0);
// read in
int read_into[sz];
if (! file.read(reinterpret_cast<char*>(read_into),
sz * sizeof *read_into))
{ std::cerr << "read failed\n"; return 2; }
for (auto const& elt: read_into) std::cout << elt << '\n';
}
|
http://coliru.stacked-crooked.com/a/4f672ea7fffc706d