Skip to content

File Output and Input article - #82

Open
thevinh23 wants to merge 2 commits into
TCCPP:mainfrom
thevinh23:main
Open

File Output and Input article#82
thevinh23 wants to merge 2 commits into
TCCPP:mainfrom
thevinh23:main

Conversation

@thevinh23

Copy link
Copy Markdown

No description provided.

Added sub-items for 'File Input and Output' section in sidebar.
```
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.

In C++, you can use the `fstream` library to create, write and read files. \
To use fstream, you need to include the `<iostream>` **AND** `<fstream>` header files.

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

## 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


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

---

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

#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::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

``` 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 {}

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants