C++ Files and Stream


File is a collection of data.


In C++ iostream standard library file, which provides cin method reading from standard input and cout method writing to standard output respectively.


The standard C++ library to read and write on the file is called fstream.

c++ file

There are three data types:

Data Types Description
ifstream The input file stream is used for the user to read information from files.
ofstream The output file stream is used to create files and to write the data to the files.
fstream The file stream used to read and write operation on the file.

Open()

The open() member function is used to open file. All the three File I/O classes ifstream, ofstrem, fstream provide to open() member function.


Syntax:

void open(const char *filename, int file_mode);

Mode Description
ios::in The input file stream is used for the user to read information from files.
ios::out The output file stream is used to create files and to write the data to the files.
ios::app The file stream used to read and write operation on the file.
ios::ate Position the file marker at end of file.
ios::trunc Delete file if it exist and recreate it.
ios::binary Open the file in binary mode

Close()

The close() member function is used to close a file which has been opened for reading and writing.

 

The close member function is automatically flushes all the streams when program terminates. Before program termination close the opened files for good practice.


Syntax:

 fileObject.close();

Example:

#include <fstream>
#include <iostream> 
using namespace std;
 
int main () {
   char ch[50];
   ofstream f1; // open a file in write mode.
   f1.open("test.txt");
   cout << "Writing to the file" << endl;
   cout << "Enter the employee name: "; 
   cin.getline(ch, 50);
   f1 << ch << endl; // write input data into the file.
   cout << "Enter your age: "; 
   cin >> ch;
   cin.ignore();
   f1 << ch << endl; // again write input data into the file.
   f1.close();// close the file.
   // open a file in read mode.
   ifstream f2; 
   string output; 
   f2.open("test.txt"); 
   cout << "Reading from a text file: " ;   
   while (getline (f2,output))  
   {  
        cout << output << endl;  
   }      
   f2.close();  
   return 0;
}

Positioning the file pointer

seekg()

The seekg() member function is used to position the file pointer to input operation.


Syntax:

fileObject.seekg( n );
fileobject.(long n, seek_direction); 

Value File Position
ios::beg from the beginning of the file
ios::cur from the current file pointer position
ios::end from the end of the file



OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.