summaryrefslogtreecommitdiff
path: root/graphs/cpp/stdin_to_file
diff options
context:
space:
mode:
authorMartial Simon <msimon_fr@hotmail.com>2025-09-15 01:08:27 +0200
committerMartial Simon <msimon_fr@hotmail.com>2025-09-15 01:08:27 +0200
commitc9b6b9a5ca082fe7c1b6f58d7713f785a9eb6a5c (patch)
tree3e4f42f93c7ae89a364e4d51fff6e5cec4e55fa9 /graphs/cpp/stdin_to_file
add: graphs et rushs
Diffstat (limited to 'graphs/cpp/stdin_to_file')
-rw-r--r--graphs/cpp/stdin_to_file/stdin_to_file.cc20
-rw-r--r--graphs/cpp/stdin_to_file/stdin_to_file.hh6
-rw-r--r--graphs/cpp/stdin_to_file/stdin_to_file_example.cc10
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;
+}