Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions wiki/cpp-tutorial/file-io.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,66 @@
---
wip: true
---

# File Input and Output
In C++, you can use the `fstream` library to create, write and read files. \

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of jumping right into "how", a brief discussion of what and why may be useful.

To use fstream, you need to include the `<iostream>` **AND** `<fstream>` header files.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You only need fstream.


``` cpp

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No space between ` and cpp

#include <iostream>
#include <fstream>
```
The `<fstream>` library provides three main classes:
|Function|Description|

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These aren't functions, these are types.

|--------|-----------|
|`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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wording feels a bit weird, I'm just not sure how to address it right now.

:::

## Read a File (`std::ifstream`) {#read-a-file}
`std::ifstream` is used to read data from a file.

``` cpp

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No space

std::string line;

std::ifstream file("filename.txt");

if (!file) return 1;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency, use {} around branches


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;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment about {}


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}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'd probably put this in a separate article. I'm not 100% sure if this is something useful for beginners. Would want to get another opinion here.

`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.
:::
20 changes: 19 additions & 1 deletion wiki/cpp-tutorial/sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down