IO#
Input/output related functions for printing, flushing, writing, reading and scanning.
Info
To use declarations from this page, include the <essence/io.hpp> header.
-
std::string io::readline(std::FILE *stream, char delimiter = '\n')#
Read from a file stream until a delimiter is reached. The default is to read until a newline character.
std::string line = io::readline(stdin);
if (result.empty())
{
io::eprintn("Got an empty line!");
return 1;
}
io::printn("You entered {:s}", line);
-
template<scannable T>
std::expected<T, error> io::scan(std::FILE *port = stdin)# Scan a line from a file stream for a scannable type. Returns an error object on failure, containing a message and error number.
auto some_integer = io::scan<i32>(stdin);
if (not some_integer)
{
// hanlde the error
io::eprintn(some_integer.error().message());
return some_integer.error().number();
}
io::printn("You entered the number {:d}", *some_integer);
-
template<scannable T>
std::expected<T, error> io::prompt(std::string_view message = "")# Print a message to
stdoutand scan a line for a scannable type. Returns an error object on failure, containing a message and error number.
auto result = io::prompt<std::string>("What is your name? ");
if (not result)
{
io::eprintn(result.error().message());
return result.error().number();
}
io::printn("Hello {:s}, nice to meet you.", *result);
-
void io::write(char c, std::FILE *port)#
Write a character to a file stream.
stdoutby default.
-
void io::write(const char *s, std::FILE *port)#
Write a string to a file stream.
stdoutby default.
-
void io::flush(std::FILE *port)#
Flush a file stream.
stdoutby default.Passing a
nullptrwill cause all ports to be flushed, as in the behaviour offflush.
-
template<typename ...Args>
void io::print(std::string_view msg, Args&&... args)# Print a string to
stdoutafter applying the format argumentsargs.Example
std::string name { "Will" }; i32 lucky_number = 7; io::print("Hello {}, your lucky number is {}\n", name, lucky_number);
-
template<typename ...Args>
void io::printn(std::string_view msg, Args&&... args)# Print a string to
stdoutwith a newline after applying the format argumentsargs.Example
std::string name { "Will" }; i32 lucky_number = 7; io::print("Hello {}, your lucky number is {}", name, lucky_number);
-
template<typename ...Args>
void io::eprint(std::string_view msg, Args&&... args)# Print a string to
stderrafter applying the format argumentsargs.
-
template<typename ...Args>
void io::eprintn(std::string_view msg, Args&&... args)# Print a string to
stderrwith a newline after applying the format argumentsargs.
-
std::string io::file::to_string(const std::string &path)#
Read a file from disk to a new string. On failure to read
path, it will return an empty string.Parameter
pathmay also be a const char*.
-
void io::file::to_string(const std::string &path, std::string &out)#
Read a file from disk into the mutable string reference
out.On failure, the string will be unchanged.
Parameter
pathmay also be a const char*.
-
std::vector<std::string> io::file::lines(const std::string &path) noexcept#
Read a file from disk into a new vector of strings, one entry for each line.
The returned vector will be empty on failure to read
path.Parameter
pathmay also be a const char*.Example
auto lines = io::file::lines("some-file.txt"); for (usize n = 1; const auto& line: lines) { io::printn("{} | {}", n, line); ++n; } // output: // // 1 | this is line one // 2 | this is line two // ...
-
void io::file::lines(const std::string &path, std::vector<std::string> &out) noexcept#
Read a file from disk into the mutable string vector
out.On failure to read
path,outwill be unchanged.Parameter
pathmay also be a const char*.