C++ Fstream Class
C++ Fstream Class
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
Example
Use fstream to read and write to a file:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
// Create and open a text file
fstream MyFile("filename.txt");
https://www.w3schools.com/cpp/ref_fstream_fstream.asp 1/20
11/3/25, 6:07 PM C++ fstream fstream class
MyFile.close();
} Tutorials References Exercises Sign In
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
fstream MyFile("filename.txt");
The fstream class has a variety of functions for reading and writing files which are listed
below.
File pointer functions are used to manipulate file pointers. There are functions for both a read
file pointer and a write file pointer, but the fstream class uses the same pointer for both
actions, so changing one of them also changes the other one.
seekg()
The seekg(position) method moves the read file pointer to a specified position relative to
the beginning of the file.
https://www.w3schools.com/cpp/ref_fstream_fstream.asp 2/20
11/3/25, 6:07 PM C++ fstream fstream class
The seekg(position, origin) method moves the read file pointer to a specified position in
the file relative to an origin. The origin has three possible values:
MyFile.seekg(6, fstream::beg);
cout << MyFile.tellg() << "\n";
MyFile.seekg(-3, fstream::cur);
cout << MyFile.tellg() << "\n";
MyFile.seekg(-4, fstream::end);
cout << MyFile.tellg() << "\n";
tellg()
The tellg() method returns the current position of the file pointer in the file.
seekp()
The seekp(position) method moves the write file pointer to a specified position relative to
the beginning of the file.
https://www.w3schools.com/cpp/ref_fstream_fstream.asp 3/20
11/3/25, 6:07 PM C++ fstream fstream class
The seekp(position, origin) method moves the write file pointer to a specified position
in the file relative to an origin. The origin has three possible values:
MyFile.seekp(6, fstream::beg);
cout << MyFile.tellp() << "\n";
MyFile.seekp(-3, fstream::cur);
cout << MyFile.tellp() << "\n";
MyFile.seekp(-4, fstream::end);
cout << MyFile.tellp() << "\n";
tellp()
The tellp() method returns the current position of the write file pointer in the file.
get()
https://www.w3schools.com/cpp/ref_fstream_fstream.asp 4/20
11/3/25, 6:07 PM C++ fstream fstream class
The get() method reads a single character from a file and returns its ASCII value as an int
Tutorials References Exercises
value. Convert it to a char type to see the character. The file pointer is moved to the next
Sign In
char destination[20];
MyFile.get(destination, 20);
cout << destination << "\n";
getline()
The getline(destination, size, delimiter) method is the same as the
get(destination, size, delimiter) method, except that the line break or delimiter is
discarded and the file pointer is moved to the character that follows it.
char destination[20];
MyFile.getline(destination, 20);
cout << destination << "\n";
https://www.w3schools.com/cpp/ref_fstream_fstream.asp 5/20
11/3/25, 6:07 PM C++ fstream fstream class
string destination;
getline(MyFile, destination);
cout << destination << "\n";
read()
The read(destination, n) method reads n characters from the file and writes them into the
char array specified by the destination parameter. Unlike other functions, it does not stop
reading at line breaks and it does not add a null terminating character to the data.
char destination[20];
MyFile.read(destination, 19);
destination[20] = '\0'; // Make sure it ends with a null terminating charac
cout << destination << "\n";
peek()
https://www.w3schools.com/cpp/ref_fstream_fstream.asp 6/20
11/3/25, 6:07 PM C++ fstream fstream class
The peek() method reads a single character from a file and returns its ASCII value as an int
Tutorials References Exercises Sign In
value. Convert it to a char type to see the character. Unlike the get() method, this method
does not move the file pointer.
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
gcount()
The gcount() method returns the number of characters extracted from the file by most
recently called file reading method.
char destination[20];
MyFile.getline(destination, 20);
cout << MyFile.gcount() << "\n";
write()
The write(str, n) method writes n characters from the char array str into the file and
moves the file pointer forward by n characters.
https://www.w3schools.com/cpp/ref_fstream_fstream.asp 7/20
11/3/25, 6:07 PM C++ fstream fstream class
put() Tutorials
References Exercises Sign In
The put(c)
HTML CSS method writes the specified
JAVASCRIPT SQL character c into
PYTHON JAVAthe file
PHPand moves
HOW the
TO file W3.CSS
pointer C
forward by one character.
open()
The open(filepath) method opens the file at the path specified by filepath. If a file is already
open then this method has no effect.
ofstream MyFile;
MyFile.open("filename.txt");
is_open()
The is_open() method returns true if a file is open and false if there is no file open.
fstream MyFile;
cout << MyFile.is_open(); << "\n"; // Displays 0 because the file is not op
MyFile.open("filename.txt");
cout << MyFile.is_open(); << "\n"; // Displays 1 because the file is open
https://www.w3schools.com/cpp/ref_fstream_fstream.asp 8/20
11/3/25, 6:07 PM C++ fstream fstream class
close()Tutorials
References Exercises Sign In
The close()
HTML CSS method closes a file.
JAVASCRIPT SQLIt is good to closeJAVA
PYTHON a file when
PHPyou are
HOWfinished
TO working
W3.CSSwith C
it to free up resources.
MyFile.close();
rdbuf()
The rdbuf() method returns a pointer to the internal filebuf object which directly handles
the file.
Syntax
It can also be used multiple times to read parts of a file one after another.
https://www.w3schools.com/cpp/ref_fstream_fstream.asp 9/20
11/3/25, 6:07 PM C++ fstream fstream class
The >> extraction operator starts by skipping over whitespace characters (spaces, tabs and line
breaks) until it reaches the first character that is not whitespace. After that, it follows the rules
shown in the following table based on the data type of the variable.
bool Reads an integer in the same way as described above and then 0
interprets 0 as false and 1 as true. Any other integer value will be 1
interpreted as true but the ifstream object will fail and stop +01
reading further.
The boolalpha manipulator described in the next section
completely changes this behavior.
string Reads all of the characters up to the next whitespace (space, tab or Hello
char * line break), null terminating character or end of file. The variable will
have a \0 null terminating character added to the value.
https://www.w3schools.com/cpp/ref_fstream_fstream.asp 10/20
11/3/25, 6:07 PM C++ fstream fstream class
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
Manipulators
A manipulator can be used in place of a variable. When manipulators are used they change how
data is interpreted by the fstream object. The effect of a manipulator remains until another
manipulator changes it.
The following table has a list of manipulators that can be used with the >> extraction operator.
Manipulator Description
noskipws Instead of skipping over whitespace characters the >> extraction operator
will read them. This is mainly useful for char type variables because with
other data types it stops reading when it runs into whitespace.
ws Moves the file pointer to the next position of the file that does not have
whitespace.
boolalpha When reading data for a boolean variable, instead of looking for an integer
it looks for the character sequence "true" or "false".
Example
https://www.w3schools.com/cpp/ref_fstream_fstream.asp 11/20
11/3/25, 6:07 PM C++ fstream fstream class
bool Tutorials
myBool; References Exercises Sign In
int myInt;
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
// Interpret character sequences "true" and "false" as boolean values
MyFile >> boolalpha >> myBool;
// Read hexadecimal numbers from the file and interpret them as integers
MyFile >> hex >> myInt;
Manipulators
Manipulators change the formatting of the data that is written to the file. They are used with
the << insertion operator in the same way as literal values and variables.
Except for setw() , the effect of a manipulator remains until another another manipulator
changes it.
https://www.w3schools.com/cpp/ref_fstream_fstream.asp 12/20
11/3/25, 6:07 PM C++ fstream fstream class
Manipulator
Tutorials References
Description
Exercises
Example
Sign In
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
boolalpha Writes boolean values as "true" and "false" MyFile <<
instead of "1" and "0". boolalpha <<
false;
https://www.w3schools.com/cpp/ref_fstream_fstream.asp 13/20
11/3/25, 6:07 PM C++ fstream fstream class
false;
Tutorials References Exercises Sign In
nouppercase Used to reset the change made by the MyFile << hex <<
uppercase manipulator. nouppercase <<
12;
https://www.w3schools.com/cpp/ref_fstream_fstream.asp 14/20
11/3/25, 6:07 PM C++ fstream fstream class
HTML CSS
setw() JAVASCRIPT SQLminimum
Specifies the PYTHON
number JAVA
of PHP HOW TO W3.CSS C
MyFile <<
characters wide the next output should be. setw(10) <<
If the output is not wide enough then "Hello";
padding is added to fill up the remaining
space.
Requires the <iomanip> library.
showpoint Always writes the decimal point for floating MyFile <<
point numbers even if it is not needed. showpoint <<
12345.0;
uppercase Represents hexadecimal digits and the MyFile << hex <<
scientific notation "e" in uppercase. uppercase << 12;
❮ fstream classes
ADVERTISEMENT
https://www.w3schools.com/cpp/ref_fstream_fstream.asp 15/20
11/3/25, 6:07 PM C++ fstream fstream class
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
https://www.w3schools.com/cpp/ref_fstream_fstream.asp 16/20
11/3/25, 6:07 PM C++ fstream fstream class
COLOR PICKER
Tutorials References Exercises Sign In
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
Recommended videos Powered by Snigel
ADVERTISEMENT
https://www.w3schools.com/cpp/ref_fstream_fstream.asp 17/20
11/3/25, 6:07 PM C++ fstream fstream class
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
REMOVE ADS
ADVERTISEMENT ADVERTISEMENT
https://www.w3schools.com/cpp/ref_fstream_fstream.asp 18/20
11/3/25, 6:07 PM C++ fstream fstream class
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
PLUS SPACES
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
https://www.w3schools.com/cpp/ref_fstream_fstream.asp 19/20
11/3/25, 6:07 PM C++ fstream fstream class
Python Reference
Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.
https://www.w3schools.com/cpp/ref_fstream_fstream.asp 20/20