libzed 1.9.9
A general-purpose library for quick and simple data manipulation.
 
Loading...
Searching...
No Matches
file.cpp
// Read all lines from a file, applying an ad-hoc mutation to them
#include <iostream>
#include <z/all.hpp>
int main() {
const auto filename = execdir() + "/../data/in1.txt";
"Original file contents:"_zs.writeln(std::cout);
for (auto line : lines(filename)) {
line.writeln(std::cout);
}
// Create a lambda that mutates each line of the file while they're read.
auto mutated_lines = lines(filename).map<zstring>([](auto line) {
auto result = line;
result.replace(' ', "˽");
result.replace('\n', "˥\n");
result.replace('\r', "˿");
result.replace('\t', "˾");
return result;
});
"\nMutated file contents:"_zs.writeln(std::cout);
for (auto line : mutated_lines) {
line.writeln(std::cout);
}
return 0;
}
array< U > map(std::function< U(const T &)> lambda) const
Applies a transformation function to each element of the array and returns a new array with the resul...
Definition array.hpp:1206
A template class for character strings.
Definition string.hpp:62
void writeln(std::ostream &stream) const noexcept
Write string data to a stream in its format, appending a newline.
Definition string.hpp:1507
zpath execdir() noexcept
Get the directory path of the running executable.
Definition execdir.hpp:11
core::generator< zstring, fileHandle > lines(const zpath &filename)
Reads the contents of a file line-by-line.