From c9b6b9a5ca082fe7c1b6f58d7713f785a9eb6a5c Mon Sep 17 00:00:00 2001 From: Martial Simon Date: Mon, 15 Sep 2025 01:08:27 +0200 Subject: add: graphs et rushs --- graphs/cpp/stdin_to_file/stdin_to_file.cc | 20 ++++++++++++++++++++ graphs/cpp/stdin_to_file/stdin_to_file.hh | 6 ++++++ graphs/cpp/stdin_to_file/stdin_to_file_example.cc | 10 ++++++++++ 3 files changed, 36 insertions(+) create mode 100644 graphs/cpp/stdin_to_file/stdin_to_file.cc create mode 100644 graphs/cpp/stdin_to_file/stdin_to_file.hh create mode 100644 graphs/cpp/stdin_to_file/stdin_to_file_example.cc (limited to 'graphs/cpp/stdin_to_file') 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 +#include +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 +#include + +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 + +#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; +} -- cgit v1.2.3