C++ Files
C++ Files
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
C++ Files
❮ Previous Next ❯
C++ Files
The fstream library allows us to work with files.
To use the fstream library, include both the standard <iostream> AND the
<fstream> header file:
Example
#include <iostream>
#include <fstream>
There are three classes included in the fstream library, which are used to create,
write or read files:
Class Description
https://www.w3schools.com/cpp/cpp_files.asp 1/5
1/26/24, 10:24 PM C++ Files
Create
and
Tutorials Write ToServices
Exercises a File Sign Up Log in
the file.
Example
#include <iostream>
#include <fstream>
using namespace std;
int main() {
// Create and open a text file
ofstream MyFile("filename.txt");
Read a File
To read from a file, use either the ifstream or fstream class, and the name of the
file.
Note that we also use a while loop together with the getline() function (which
belongs to the ifstream class) to read the file line by line, and to print the content of
the file:
https://www.w3schools.com/cpp/cpp_files.asp 2/5
1/26/24, 10:24 PM C++ Files
Tutorials
Example
Exercises Services Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
// Create a text string, which is used to output the text file
string myText;
// Use a while loop together with the getline() function to read the file
line by line
while (getline (MyReadFile, myText)) {
// Output the text from the file
cout << myText;
}
Run example »
https://www.w3schools.com/cpp/cpp_files.asp 3/5
1/26/24, 10:24 PM C++ Files
COLOR PICKER
Tutorials Exercises Services Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
REPORT ERROR
Top Tutorials
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
How To Tutorial
SQL Tutorial
Python Tutorial
W3.CSS Tutorial
Bootstrap Tutorial
PHP Tutorial
Java Tutorial
C++ Tutorial
jQuery Tutorial
Top References
HTML Reference
CSS Reference
JavaScript Reference
SQL Reference
Python Reference
W3.CSS Reference
Bootstrap Reference
https://www.w3schools.com/cpp/cpp_files.asp 4/5
1/26/24, 10:24 PM C++ Files
PHP Reference
HTML Colors
Tutorials
Java Reference
Exercises Services Sign Up Log in
Angular Reference
HTML
CSS jQuery Reference
JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
Top Examples Get Certified
HTML Examples HTML Certificate
CSS Examples CSS Certificate
JavaScript Examples JavaScript Certificate
How To Examples Front End Certificate
SQL Examples SQL Certificate
Python Examples Python Certificate
W3.CSS Examples PHP Certificate
Bootstrap Examples jQuery Certificate
PHP Examples Java Certificate
Java Examples C++ Certificate
XML Examples C# Certificate
jQuery Examples XML Certificate
FORUM ABOUT
W3Schools is optimized for learning and training. Examples might be simplified to
improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we
cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted our
terms of use, cookie and privacy policy.
https://www.w3schools.com/cpp/cpp_files.asp 5/5