Home >>C++ Tutorial >C++ File Streams
File streams in C++ are basically the libraries that are used in the due course of programming. The programmers generally use the iostream standard library in the C++ programming as it provides the cin and cout methods that are used for reading from the input and writing to the output respectively.
In order to read and write from a file, the programmers are generally using the standard C++ library that is known as the fstream.
Here is the list of the data types that are defined in the fstream library:
Data Type | Description |
---|---|
fstream | This data types is generally used to create files, write information to files, and read information from files. |
ifstream | This data types is generally used to read information from files. |
ofstream | This data types is generally used to create files and write information to the files. |
#include <iostream> #include <fstream> using namespace std; int main () { ofstream filestream("test.txt"); if (filestream.is_open()) { filestream << "Welcome to the world of C++ Tutorial.\n"; filestream << "Hello user.\n"; filestream.close(); } else { cout <<"No Such File created."; } return 0; }
#include <iostream> #include <fstream> using namespace std; int main () { string srg; ifstream filestream("test.txt"); if (filestream.is_open()) { while ( getline (filestream,srg) ) { cout << srg <<endl; } filestream.close(); } else { cout << "No such file found."<<endl; } return 0; }