×

C++ File Handling

File handling is a mechanism that manipulates the data stored in files. File handling store output data from the program to external file and read file data to the program. There are two types of communication takes place program.
  1. Data transfer between the console unit and program.
  2. Data transfer between program and disk file.

C++ Standard File Handling Class

C++ contains a set of library classes that handle the file handling methods. These classes include:
  1. ifstream: this header file is used to read input file stream from the file using istream.
  2. ofstream: this header file is used to write output file stream into the file using ostream.
  3. fstream: this header file is used for both input and output operation and inherits all the function of istream and ostream thorough iostream.
The above classes are derived from fstreambase and from the corresponding iostream class.

Input and Output Stream

The stream that supplies data from file to program is known as input stream. Input stream is used to read the file into program. The stream that supplies data from the program to file is known as an output stream. The output stream is used to write from the program into file. C++ file handling header files contain several functions for different operations.
Function Description
open() To create a new file as well as open an existing file
close() To close an existing file
read() Read data from the file
write() Write data into the file
get() Read a single character from a file
put() Write a single character into a file

C++ File Handling Mode Parameter

Mode parameter in the file handling defines the specific purpose for which the file is opened. The mode parameter is always passed as the second parameter in the file handling methods. These modes are:
Mode Description
Ios::in Open file in read mode
Ios::out Open file in write mode
Ios::app Append to end of file
Ios::ate Go to end of file on opening
Ios::trunk Delete the content of the file if it exits

C++ File Handling open() Function

A file must be opened before doing any manipulation over it. The open() function is used to open a file either for read from or write into a file. Syntax
file-stream-class   file-stream-object;  
file-stream-object.open("filename");
Example
ofstream  outfile;        
outfile . open ("employee.txt");

C++ File Handling open() Function Example

#include <iostream>  
#include <fstream>  
using namespace std;  
int main () {  
  ofstream myfile;  
// open file employee.txt  
  myfile.open ("D:/Ashutosh/C++ Tutorial/C++ tutorial/employee.txt");// 
check file location  
  if(myfile.is_open()){  
    cout<<"File is open"<<endl;  
    myfile << "Writing into a file.\n";  
    myfile.close();  
  }  
  else{  
    cout<<"Error in file opening"<<endl;  
  }  
  return 0;  
}
Output: 


Related Topics

C++ Fork

A new process known as a "child process" is created with the fork system function and runs concurrently with the process that invoked fork() (parent process). Both processes will carry...

4 minutes read.

C++ Memory Management

Memory management is a method of controlling computer memory and allocating memory space to applications to increase overall system performance. What is the purpose of memory management? Because the array contains homogeneous...

4 minutes read.

Multiset in C++

Introduction Multisets are part of the C++ STL, or Standard Template Library. In C++, a multiset is a set of associative containers that hold ordered items. Items in a multiset can...

10 minutes read.

C++ Expressions

C ++ equations are made up of operators, constants and variables arranged according to language rules. It may also include function calls that give results. To calculate the value, the...

4 minutes read.

4 Pillars of OOPs

OOPs OOPS means Object Oriented Programming System Structure. Object oriented Programming is defined as an approach that provides a way of modularizing programs by creating partitioned memory area for both data...

11 minutes read.

Delete Operator in C++

Overview We can reserve allocation for a variable or an array at runtime in C++. Dynamic memory allocation is the term for this. After utilising a variable in C++, we must...

4 minutes read.

Decltype type Specifier in C++

The primary use of C++ decltype is to inspect the declaration type of an entity in an expression. The auto keyword can declare a particular type of variable, whereas the...

4 minutes read.

Implementing the sets without C++ STL containers

Many practical features and tools in C++ support us in programming competitions. One of these parts is a set from the Standard Template Library (STL), which offers an effective way...

6 minutes read.

Private Inheritance in C++

Private inheritance is an inheritance in object-oriented programming (OOP) languages where a subclass derives from a superclass. Still, the derived class does not inherit the public and protected members of...

7 minutes read.

Multiple Inheritance

C++ Multiple Inheritance Multiple inheritance is such an inheritance in which a derived class inherits properties of more than one base class.     C++ Multiple Inheritance Example In this example, two base classes Square and...

2 minutes read.

Structure Vs Class in C++

The structure in C++ is similar to that of a class, with a few exceptions. Both the structure and the stage, the most important thing is safety. The property is...

4 minutes read.

C++ Program to find the element that occurs once

Write a program to find the element in the array that occurs once. Given that all the numbers in the array are present two times and the array is sorted,...

7 minutes read.

How to call a void function in C++

Generally, any function has two types: 1. Void function: It doesn't return any value. 2. Non-void function: It returns some value. Program to call a void function in C++ #include <iostream> using namespace std;  void check() {  ...

2 minutes read.

C++ String

A string is a collection of characters. C++ programming language supports both C string as well as standard C++ library string. In C++, string is an object of std::string class. C Style String The C style...

5 minutes read.

Upcasting and Downcasting in C++

With the help of various examples in the C++ programming language, this section will cover Upcasting and Downcasting. Upcasting and downcasting, on the other hand, are two forms of object typecasting. Consider...

3 minutes read.

Approach in C++

Object oriented programming languages like Java or C++ use a bottom-up approach that identifies each object first.  In the bottom-up approach, we create a small problem first, and try to...

6 minutes read.

Malloc() and new in C++

In C ++, malloc () and new are used for the same thing. During runtime, they are used to allocate memory. Malloc () and the new, on the other hand,...

4 minutes read.

C++ For loop

C++ loop Statement C++ Loop statement allows us to repeat the execution of a statement or group of statements multiple times. The statement(s) repeat execution within loop until the condition of loop...

2 minutes read.

Program to convert infix to postfix expression in C++

Parentheses are frequently employed in mathematical formulas to make their interpretation easier to understand. However, with computers, parenthesis in an expression might lengthen the time it takes to find a...

7 minutes read.

Differences between Local and Global Variable

Define Global Variable Global variables are those that may be accessible worldwide across a programme and are defined outside of any functions or blocks. It may be accessed by any function in...

3 minutes read.