From 4ffe05c74b078675019cdce97add647553dd6701 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=B9i=20Th=E1=BA=BF=20Vinh?= <93034845+thevinh23@users.noreply.github.com> Date: Sun, 19 Apr 2026 20:51:29 +0700 Subject: [PATCH 1/2] Added sub-items for 'File Input and Output' Added sub-items for 'File Input and Output' section in sidebar. --- wiki/cpp-tutorial/sidebar.ts | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/wiki/cpp-tutorial/sidebar.ts b/wiki/cpp-tutorial/sidebar.ts index 019b2e4..6b40175 100644 --- a/wiki/cpp-tutorial/sidebar.ts +++ b/wiki/cpp-tutorial/sidebar.ts @@ -134,7 +134,25 @@ const sidebar = [ ], collapsed: true, }, - { text: "File Input and Output", link: "/cpp-tutorial/file-io" }, + { + text: "File Input and Output", + link: "/cpp-tutorial/file-io", + items: [ + { + text: "Read a File", + link: "/cpp-tutorial/file-io#read-a-file", + }, + { + text: "Write a File", + link: "/cpp-tutorial/file-io#write-a-file", + }, + { + text: "Redirecting standard input/output", + link: "/cpp-tutorial/file-io#redirecting-stdio", + }, + ], + collapsed: true; + }, { text: "User Defined Types", link: "/cpp-tutorial/user-defined-types", From dfa62e9a44876a17173f91faa81f7372d012110c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=B9i=20Th=E1=BA=BF=20Vinh?= <93034845+thevinh23@users.noreply.github.com> Date: Sun, 19 Apr 2026 16:14:27 +0000 Subject: [PATCH 2/2] Added 'File Input and Output' documentation --- wiki/cpp-tutorial/file-io.md | 63 ++++++++++++++++++++++++++++++++++++ wiki/cpp-tutorial/sidebar.ts | 2 +- 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/wiki/cpp-tutorial/file-io.md b/wiki/cpp-tutorial/file-io.md index dae69fc..07115a1 100644 --- a/wiki/cpp-tutorial/file-io.md +++ b/wiki/cpp-tutorial/file-io.md @@ -1,3 +1,66 @@ --- wip: true --- + +# File Input and Output +In C++, you can use the `fstream` library to create, write and read files. \ +To use fstream, you need to include the `` **AND** `` header files. + +``` cpp +#include +#include +``` +The `` library provides three main classes: +|Function|Description| +|--------|-----------| +|`std::ofstream`|Used to create and write to files| +|`std::ifstream`|Used to read from files| +|`std::fstream`|Used for both reading and writing| + +::: info Note +`std::fstream` is a general-purpose file stream, while `std::ifstream` and `std::ofstream` are more specific and commonly used in practice. +::: + +## Read a File (`std::ifstream`) {#read-a-file} +`std::ifstream` is used to read data from a file. + +``` cpp +std::string line; + +std::ifstream file("filename.txt"); + +if (!file) return 1; + +while (std::getline(file, line)) { + std::cout << line << "\n"; +} + +file.close(); +``` + +Using a `while` loop with `getline()` allows us to read a file line by line until we reach the end, and then print each line. + +## Write a File (`std::ofstream`) {#write-a-file} +`std::ofstream` is used to write data to a file. + +``` cpp +std::ofstream file("filename.txt"); + +if (!file) return 1; + +file << "Hello Together C & C++!"; + +file.close(); +``` +This will create the file if it doesn’t exist, or overwrite it if it already exists. + +## Redirecting standard input/output {#redirecting-stdio} +`freopen` is used to redirect standard input and output streams to files.\ +It reassigns `stdin` or `stdout` to a file instead of the default console input/output. +``` cpp +freopen("input.txt", "r", stdin); +freopen("output.txt", "w", stdout); +``` +::: info Note +`freopen` is commonly used in competitive programming for convenience, but it is not recommended for general C++ applications. +::: \ No newline at end of file diff --git a/wiki/cpp-tutorial/sidebar.ts b/wiki/cpp-tutorial/sidebar.ts index 6b40175..3feda6b 100644 --- a/wiki/cpp-tutorial/sidebar.ts +++ b/wiki/cpp-tutorial/sidebar.ts @@ -151,7 +151,7 @@ const sidebar = [ link: "/cpp-tutorial/file-io#redirecting-stdio", }, ], - collapsed: true; + collapsed: true, }, { text: "User Defined Types",