When I open an ofstream object for a file I do:
std::ofstream outFile("myfile");
Then I want to want to some numbers to it, but the only method it has is the ::write()
method which takes a const char*
and a size. So I could do:
int temp = 5;
outFile.write((const char*)&temp, sizeof(int));
There's also the input operator <<
, but I don't want formatted input, I want it written as pure binary data. I was wondering if there's a way I can write directly like:
outFile.write(12, sizeof(int));
outFile.write(10, sizeof(int));
Instead of creating temporaries, or previously allocating an array. I'm thinking that the limitation is that the only value that I could put in the argument would be an RValue, and there doesn't seem there's a way of making an lvalue from an rvalue. I guess technically the rvalue lasts until the end of the line, so if there was the opposite of std::move
it could work. Is it possible to do this writing numbers directly?