libostd
Classes | Enumerations | Functions | Variables
io.hh File Reference

File streams and standard output/input/error manipulation. More...

Go to the source code of this file.

Classes

struct  ostd::file_stream
 A file stream. More...
 

Enumerations

enum  ostd::stream_mode {
  ostd::stream_mode::READ = 0, ostd::stream_mode::WRITE, ostd::stream_mode::APPEND, ostd::stream_mode::READ_U,
  ostd::stream_mode::WRITE_U, ostd::stream_mode::APPEND_U
}
 The mode to open file streams with. More...
 

Functions

void ostd::swap (file_stream &a, file_stream &b)
 Swaps two file streams including ownership.
 
template<typename ... A>
void ostd::write (A const &...args)
 Writes all given values into standard output. More...
 
template<typename ... A>
void ostd::writeln (A const &...args)
 Writes all given values into standard output followed by a newline. More...
 
template<typename ... A>
void ostd::writef (string_range fmt, A const &...args)
 Writes a formatted string into standard output. More...
 
template<typename ... A>
void ostd::writefln (string_range fmt, A const &...args)
 Writes a formatted string into standard output followed by a newline. More...
 

Variables

OSTD_EXPORT file_stream ostd::cin
 Standard input file stream.
 
OSTD_EXPORT file_stream ostd::cout
 Standard output file stream.
 
OSTD_EXPORT file_stream ostd::cerr
 Standard error file stream.
 

Detailed Description

File streams and standard output/input/error manipulation.

This file implements a file stream structure equivalent to the C FILE as well as wrappers over standard input/output/error and global functions for formatted writing into standard output.

Some string examples:

#include <vector>
#include <ostd/string.hh>
#include <ostd/io.hh>
using namespace ostd;
int main() {
writeln("writing sample file...");
file_stream wtest{"test.txt", stream_mode::WRITE};
std::string smpl =
"This is a test file for later read.\n"
"It contains some sample text in order to see whether "
"things actually read correctly.\n\n\n"
""
"This is after a few newlines. The file continues here.\n"
"The file ends here.\n";
copy(iter(smpl), wtest.iter());
wtest.close();
file_stream test{"test.txt"};
writeln("## WHOLE FILE READ ##\n");
auto fr = test.iter();
"-- str beg --\n%s-- str end --",
std::string{fr.iter_begin(), fr.iter_end()}
);
test.seek(0);
writeln("\n## PART FILE READ ##\n");
auto fr2 = test.iter().take(25);
"-- str beg --\n%s\n-- str end --",
std::string{fr2.iter_begin(), fr2.iter_end()}
);
test.seek(0);
writeln("\n## BY LINE READ ##\n");
for (auto const &line: test.iter_lines()) {
writeln("got line: ", line);
}
test.close();
writeln("\n## FILE SORT ##\n");
wtest.open("test.txt", stream_mode::WRITE);
smpl = "foo\n"
"bar\n"
"baz\n"
"test\n"
"this\n"
"will\n"
"be\n"
"in\n"
"order\n";
copy(iter(smpl), wtest.iter());
wtest.close();
test.open("test.txt");
auto lns = test.iter_lines();
std::vector<std::string> x{lns.iter_begin(), lns.iter_end()};
writeln("before sort: ", x);
sort(iter(x));
writeln("after sort: ", x);
return 0;
}

And binary examples:

#include <ostd/platform.hh>
#include <ostd/io.hh>
using namespace ostd;
inline void print_result(uint32_t x) {
writefln("got x: 0x%X", x);
}
int main() {
file_stream wtest{"test.bin", stream_mode::WRITE};
iter({ 0xABCD1214, 0xBADC3264, 0xDEADBEEF, 0xBEEFDEAD }),
wtest.iter<uint32_t>()
);
wtest.close();
file_stream rtest{"test.bin"};
writefln("stream size: %d", rtest.size());
for (uint32_t x: map(rtest.iter<uint32_t>(), from_big_endian<uint32_t>())) {
print_result(x);
}
return 0;
}