-
Notifications
You must be signed in to change notification settings - Fork 20
File Output and Input article #82
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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. \ | ||
| To use fstream, you need to include the `<iostream>` **AND** `<fstream>` header files. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You only need |
||
|
|
||
| ``` cpp | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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| | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For consistency, use |
||
|
|
||
| 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; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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} | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
| ::: | ||
There was a problem hiding this comment.
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.