File Stream in C++
In order to permanently save data on a computer, file handling is employed. We can save our data in secondary memory (the hard drive) using file handling.
The actions below must be taken to achieve file handling:
1: Naming a file
2: Opening a file
3: Writing data into the file
4: Data reading from the file
5: Sealing the file.
Streams in c++:
With streams in C++, we provide input to the running program, and the running program returns the result. The term "stream" refers to both the series of bytes that a program receives as input and the sequence of bytes that it outputs. Consequently, streams are simply the sequential movement of data.
"Console I/O operation" refers to communication between a running program and input and output devices like a keyboard and display. "Disc I/O operation" refers to the input and output between the running program and files.
File stream operations classes:
A group of classes in the C++ I/O system specify the file handling methods. These include the classes ifstream, ofstream, and fstream. These classes are descended from the stream class and its equivalent iostream class. The stream file, which includes the declarations for the classes used to handle the disc files, must be included by any program that uses files.
1. ios:-
The input-output stream is referred to as ios.
In this class hierarchy, this class serves as the root class for all other classes.
This class includes all of the capabilities required for input and output operations needed by all other derived classes.
2. istream:-
The input stream is referred to as a stream.
This class is a descendant of the 'ios' class.
Stream input is handled by this class.
This class uses an overloaded extraction operator (>>) to handle input streams from files during program execution.
The get(), getline(), and read() input functions are declared in this class.
3. ostream:
The output stream is referred to as ostream.
This class is a descendant of the 'ios' class.
Stream output is handled by this class.
The insertion operator() is overloaded in this class to handle output streams to files that result from the running of programs.
The put() and write() output functions are declared in this class.
4. Streambuf :-
A pointer to the buffer that controls the input and output streams is present in this class.
5. fstreambase:-
This class offers functions that are common to file streams. Acts as the foundation for the classes fstream, ifstream, and stream.
This class includes the functions open() and shut().
6. Casestream:-
This class offers operations for input.
With default input mode, it contains the open() function.
inherits from the istream the procedures get(), getline(), read(), seek(), and tell().
7. ofstream:-
This class offers operations for output.
It includes the default output mode for the open() function.
inherits from the ostream the procedures put(), write(), seek(), and tell().
8. fstream:-
Support for simultaneous input and output operations is provided by this class.
Inherits all of the stream and ostream class's features via iostream.
9. filebuf:-Setting the file buffers to read and write is its goal.
The file buffer member function can be used to calculate the file's size as well.
Three classes—fstream, ifstream, and of the stream—that are present in the stream header file are primarily used in C++ to deal with files.
ofstream: A file-writing stream class
Ifstream :A class for reading files from streams.
fstream : A kind of stream that can read and write to/from files.
The first step is to open the specific file for read-only or write-access purposes. By 1. providing the file name in the constructor when creating the object and 2. using the open method, we can open files.
MODES:
Member Constant | Stands for | Access |
in* | input | File accessible for reading Operations for input is supported via the internal stream buffer. |
out | output | File open for writing: output operations are supported by the internal stream buffer. |
binary | binary | Instead of using text, operations are conducted in binary mode. |
ate | At end | The file's end is where the output position begins. |
app | append | The file's existing contents are added to at the end of each output operation that takes place. |
trunc | truncate | Before the file was opened, all of its contents were removed. |
Default Open Modes:
Ifstream | ios::in |
Ofstream | ios::out |
Fstream | ios::in | ios::out |
Writing a Text File:
//writing on a text file
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
Ofstream myfile(“example.txt”);
if(myfile.is_open())
{
myfile<<”This is a line.\n”;
myfile<<”This is another line.\n”;
cout<<”File Written successfully”;
myfile.close();
}
else
cout<<”Unable to open file”;
return 0;
}
![File Stream in C++](https://static.tutorialandexample.com/cpp/file-stream-in-cpp1.png)
Reading a Text File
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while ( getline (myfile, line) )
{
cout << line << '\n';
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
![File Stream in C++](https://static.tutorialandexample.com/cpp/file-stream-in-cpp2.png)