libostd/examples/stream1.cc

33 lines
710 B
C++
Raw Normal View History

2017-05-03 02:14:27 +02:00
/** @example stream1.cc
*
* An example of using streams to read and write binary files
* with some help from the standard range algorithms.
*/
2017-02-08 01:06:50 +01:00
#include <ostd/platform.hh>
2016-02-02 00:10:05 +01:00
#include <ostd/io.hh>
2016-01-31 23:19:23 +01:00
using namespace ostd;
inline void print_result(uint32_t x) {
2016-01-31 23:19:23 +01:00
writefln("got x: 0x%X", x);
}
int main() {
2017-02-16 20:39:05 +01:00
file_stream wtest{"test.bin", stream_mode::WRITE};
copy(
iter({ 0xABCD1214, 0xBADC3264, 0xDEADBEEF, 0xBEEFDEAD }),
2017-01-30 19:19:09 +01:00
wtest.iter<uint32_t>()
);
2016-01-31 23:19:23 +01:00
wtest.close();
2017-02-16 20:39:05 +01:00
file_stream rtest{"test.bin"};
2016-02-26 16:19:54 +01:00
writefln("stream size: %d", rtest.size());
2016-01-31 23:19:23 +01:00
2017-02-16 20:39:05 +01:00
for (uint32_t x: map(rtest.iter<uint32_t>(), from_big_endian<uint32_t>())) {
2016-01-31 23:19:23 +01:00
print_result(x);
}
2016-01-31 23:19:23 +01:00
return 0;
2016-02-07 22:17:15 +01:00
}