diff options
Diffstat (limited to 'graphs/cpp/stdin_to_file')
| -rw-r--r-- | graphs/cpp/stdin_to_file/stdin_to_file.cc | 20 | ||||
| -rw-r--r-- | graphs/cpp/stdin_to_file/stdin_to_file.hh | 6 | ||||
| -rw-r--r-- | graphs/cpp/stdin_to_file/stdin_to_file_example.cc | 10 |
3 files changed, 36 insertions, 0 deletions
diff --git a/graphs/cpp/stdin_to_file/stdin_to_file.cc b/graphs/cpp/stdin_to_file/stdin_to_file.cc new file mode 100644 index 0000000..c1519e3 --- /dev/null +++ b/graphs/cpp/stdin_to_file/stdin_to_file.cc @@ -0,0 +1,20 @@ +#include "stdin_to_file.hh" + +#include <fstream> +#include <iostream> +long int stdin_to_file(const std::string& filename) +{ + std::ofstream file_out; + std::string token; + // trying to open output file "data.out" + file_out.open(filename); + size_t res = 0; + // Read until eof or an error occurs + while (std::cin >> token) + { + res++; + file_out << token << '\n'; + } + // stream is closed at the end of the scope + return res; +}
\ No newline at end of file diff --git a/graphs/cpp/stdin_to_file/stdin_to_file.hh b/graphs/cpp/stdin_to_file/stdin_to_file.hh new file mode 100644 index 0000000..9aac89d --- /dev/null +++ b/graphs/cpp/stdin_to_file/stdin_to_file.hh @@ -0,0 +1,6 @@ +#pragma once + +#include <fstream> +#include <iostream> + +long int stdin_to_file(const std::string& filename); diff --git a/graphs/cpp/stdin_to_file/stdin_to_file_example.cc b/graphs/cpp/stdin_to_file/stdin_to_file_example.cc new file mode 100644 index 0000000..3bc211d --- /dev/null +++ b/graphs/cpp/stdin_to_file/stdin_to_file_example.cc @@ -0,0 +1,10 @@ +#include <iostream> + +#include "stdin_to_file.hh" + +int main() +{ + auto word_count = stdin_to_file("file.out"); + std::cout << "File has " << word_count << " words\n"; + return 0; +} |
