0% found this document useful (0 votes)
23 views5 pages

C++ Files

Uploaded by

ramukaka777787
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views5 pages

C++ Files

Uploaded by

ramukaka777787
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1/26/24, 10:24 PM C++ Files

 Tutorials  Exercises  Services   Sign Up Log in

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

ofstream Creates and writes to files

ifstream Reads from files

fstream A combination of ofstream and ifstream: creates, reads, and


writes to files

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

HTML CSSa file,JAVASCRIPT


To create use either the SQL PYTHON
ofstream JAVAclass,PHP
or fstream HOW TO
and specify W3.CSS
the name of C

the file.

To write to the file, use the insertion operator ( << ).

Example
#include <iostream>
#include <fstream>
using namespace std;

int main() {
// Create and open a text file
ofstream MyFile("filename.txt");

// Write to the file


MyFile << "Files can be tricky, but it is fun enough!";

// Close the file


MyFile.close();
}

Why do we close the file?


It is considered good practice, and it can clean up unnecessary memory space.

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;

// Read from the text file


ifstream MyReadFile("filename.txt");

// 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;
}

// Close the file


MyReadFile.close();

Run example »

❮ Previous Log in to track progress Next ❯

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



 SPACES UPGRADE AD-FREE

NEWSLETTER GET CERTIFIED

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.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by


W3.CSS.

https://www.w3schools.com/cpp/cpp_files.asp 5/5

You might also like