- C++ Programming Basics
- C++ Tutorial
- C++ Environment Setup
- C++ Character Set
- C++ Keywords
- C++ Identifiers
- C++ Constants
- C++ Punctuators
- C++ Program Structure
- C++ Basic Syntax
- C++ Comments
- C++ Basic Programs
- C++ Input Output Operator
- C++ Input Output Stream
- C++ Type & Variable
- C++ Data Types
- C++ Data Type Modifiers
- C++ Variables
- C++ Variable Types
- C++ Variable Scope
- C++ Storage Classes
- C++ Formatting Output
- C++ Operators
- C++ Operators
- C++ Type Conversion
- C++ Numbers
- C++ Assignment Operator
- C++ Shorthand
- C++ Flow of Control
- C++ Statements
- C++ Flow Control
- C++ Decision Making
- C++ if if-else if-else-if switch
- C++ Loops
- C++ for while do-while Loop
- C++ break continue goto
- C++ Standard Library
- C++ Standard Library
- C++ Header Files
- C++ Character String
- C++ Mathematical Functions
- C++ Functions
- C++ Functions
- C++ Function Types
- C++ Function Prototype
- C++ Function Call
- C++ Function Return
- C++ Friend Function
- C++ Scope Rules
- C++ Arrays & Strings
- C++ Arrays
- C++ One Dimensional Arrays
- C++ Two Dimensional Arrays
- C++ Strings
- C++ Data Structure
- C++ Data Structure
- C++ Access Structure Member
- C++ Nested Data Structure
- C++ Structure Array
- C++ Pass Structure to Function
- C++ typedef
- C++ #define
- C++ Programming Pointers
- C++ Pointers
- C++ Memory Map
- C++ Free Store
- C++ Declare Initialize Pointers
- C++ Dynamic Memory Allocation
- C++ Pointers & Arrays
- C++ Pointers & Const
- C++ Pointers & Functions
- C++ Pointers & Structures
- C++ Objects as Function Arguments
- C++ Pointers & Objects
- C++ References
- C++ File Handling
- C++ File Handling
- C++ File Streams
- C++ Data Files
- C++ Opening & Closing Files
- C++ Steps to Process Files
- C++ Change Stream Behaviour
- C++ Sequential I/O Operations
- C++ Detecting EOF
- C++ File Pointers Random Access
- C++ Binary Files Operations
- C++ Error Handling
- C++ Object Oriented
- C++ Object Oriented
- C++ Function Overloading
- C++ Classes & Objects
- C++ Constructors & Destructors
- C++ Inheritance
- C++ Encapsulation
- C++ Polymorphism
- C++ Data Abstraction
- C++ Interfaces
- C++ Programming Advance
- C++ Linked Lists
- C++ Stacks
- C++ Queues
- C++ Date Time
- C++ Preprocessors
- C++ Exception Handling
- C++ Namespaces
- C++ Dynamic Memory
- C++ Multithreading
- C++ Templates
- C++ Signal Handling
- C++ Web Programming
- C++ Programming Examples
- C++ Programming Examples
- C++ Programming Test
- C++ Programming Test
- Give Online Test
- All Test List
C++ File Handling
Sometime, it is important to store the information entered by the user into the file for further use. After storing the information into the file, later you can retrieve those information from that file. File helps in storing the information permanently.
A file itself is a bunch of bytes stored on some storage devices like taps, or magnet disk etc. In C++, the file input/output operation are performed through a component header file fstream.h of C++ standard library.
In C++, a file
- at its lowest level - is interpreted simply as a sequence, or stream, of bytes.
- at the user level - consists of a sequence of intermixed data types such as characters, arithmetic values, class objects
The fstream.h library predefines a set of operations for handling file related input and output operation. It defines certain classes that helps in performing the file input and output operations.
You will learn all about file handling in C++. C++ file handling divided into separate chapters such as
- C++ File Handling
- C++ File Streams
- C++ Data Files
- C++ Opening & Closing Files
- C++ Steps to Process Files
- C++ Change Stream Behaviour
- C++ Sequential Input/Output Operations with Files
- C++ Detecting EOF
- C++ File Pointers Random Access
- C++ Binary Files Operations
- C++ Error Handling During File Operations
C++ File Handling Programs
Let's take some example to know, how file handling is useful in C++ to store the information entered by the user for later use. Here are some sample programs listed, demonstrating how to handle files in C++. You will learn all about handling the file in C++ in separate chapters as mentioned above. If you have previous knowledge on C++ file handling, then you will find better to read examples listed below. Otherwise, just see the examples, and follow next tutorial. After sometime, you will also find better and ease in handling files in C++.
/* C++ File Handling - Store information into the file in C++ */ #include<iostream.h> #include<conio.h> #include<fstream.h> void main() { ofstream fout; char fname[20]; char rec[80]; clrscr(); cout<<"Enter file name: "; cin>>fname; fout.open(fname, ios::out); cout<<"Enter a word: "; cin>>rec; fout<<rec; cout<<"Data inserted successfully..!!"; fout.close(); getch(); }
Here, this C++ program ask you to enter a filename to create/overwrite the file and then ask to enter a word to store this word in the file. Here is the sample run of the above C++ program:

After running the above program, with entering the same information shown in the above output. You will find, a file named "myfile.txt" having "programmer" written on this file. Therefore, you are always free to retrieve this word from this file later.
Let's take another example to know how to retrieve this word.
/* C++ File Handling - Retrieve information from the file in C++ */ #include<iostream.h> #include<conio.h> #include<fstream.h> void main() { ifstream fin; char fname[20]; char rec[80]; clrscr(); cout<<"Enter file name: "; cin>>fname; cout<<"The file contains:\n"; fin.open(fname, ios::in); fin.get(rec, 80); cout<<rec; fin.close(); getch(); }
Here is the sample output of the above C++ program:

As you can see from the above output, the word is retrieved from the file (named myfile.txt) that we have used earlier to store the word (named programmer). Let's take an another example. This is the modified and complete version of the above two programs.
/* C++ File Handling - This program demonstrates * how to handle file in C++. That is, how to * write some content to a file and retrieve * the content from the file to display it on * the screen in C++. */ #include<iostream.h> #include<conio.h> #include<fstream.h> #include<stdlib.h> void main() { char rec[80], ch; ofstream fout("file.txt", ios::out); clrscr(); cout<<"Enter 4 lines of information: \n"; for(int i=0; i<4; i++) { cin.get(rec, 80); cin.get(ch); fout<<rec<<"\n"; } fout.close(); ifstream fin("file.txt", ios::in); fin.seekg(0); cout<<"\n"; for(i=0; i<4; i++) { fin.get(rec, 80); fin.get(ch); cout<<rec<<"\n"; } fin.close(); getch(); }
Here is the sample run of this C++ program:

Here is the complete version of the above programs. This program is the complete version of the C++ file handling program.
/* C++ File Handling - This is the complete * version of the C++ file handling program. * This program creates a file (entered by user) * and store some content (entered by user). * Then display those content (if user want) * on the output screen in C++ */ #include<iostream.h> #include<conio.h> #include<fstream.h> #include<stdlib.h> void main() { char rec[80], ch; char fname[20]; int count=0, i; char ans='y'; clrscr(); cout<<"Enter file name: "; cin.get(fname, 20); ofstream fout(fname, ios::out); if(!fout) { cout<<"Error in creating the file..!!\n"; cout<<"Press any key to exit...\n"; getch(); exit(1); } cin.get(ch); cout<<"Enter information to store..\n"; while(ans=='y' || ans=='Y') { cin.get(rec, 80); fout<<rec<<"\n"; cout<<"Want to enter more ? (y/n).. "; cin>>ans; count++; cin.get(ch); } cout<<"\nThe information successfully stored in the file..!!\n"; fout.close(); cin.get(ch); cout<<"Want to see ? (y/n).."; cin>>ans; if(ans=='y' || ans=='Y') { ifstream fin(fname, ios::in); if(!fin) { cout<<"Error in opening the file..!!\n"; cout<<"Press any key to exit..\n"; getch(); exit(2); } fin.seekg(0); cout<<"\n"; cout<<"The file contains:\n"; for(i=0; i<count; i++) { fin.get(rec, 80); fin.get(ch); cout<<rec<<"\n"; } fin.close(); } getch(); }
Here are the two sample run of the above C++ program:


More Examples
Here are some more examples listed for file handling, that you can go for:
- Read File
- Write to File
- Read & Display File
- Copy File
- Merge two File
- List Files in Directory
- Delete File
- Encrypt and Decrypt Files
« Previous Tutorial Next Tutorial »