100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.2 TrustPilot
logo-home
Class notes

File Handling in C++

Rating
-
Sold
-
Pages
6
Uploaded on
27-08-2025
Written in
2025/2026

File Handling in C++ in detail

Institution
Course









Whoops! We can’t load your doc right now. Try again or contact support.

Written for

Course

Document information

Uploaded on
August 27, 2025
Number of pages
6
Written in
2025/2026
Type
Class notes
Professor(s)
Monika sharma
Contains
All classes

Subjects

Content preview

File Handling
Files are used to store data permanently in a computer. Using file
handling we can store our data in secondary memory (Hard disk).
For achieving file handling we need to follow the following steps:-
STEP 1-Naming a file
STEP 2-Opening a file
STEP 3-Writing data into the file
STEP 4-Reading data from the file
STEP 5-Closing a file.

Classes for File stream operations :-

In C++, files are mainly dealt by using three classes fstream, ifstream,
ofstream available in fstream headerfile and therefore we must include
this file in any program that uses files.

ofstream: Stream class to write on files
ifstream: Stream class to read from files
fstream: Stream class to both read and write from/to files.

Streams:-The sequence of bytes given as input to the executing
program and the sequence of bytes that comes as output from the
executing program are called stream. In other words, streams are
nothing but the flow of data in a sequence.

To open the particular file for read or write operation. We can open file
by -
1. passing file name in constructor at the time of object creation
2. using the open method

, 1. Read and Write in File using Constructor
Example
#include<iostream>
#include<fstream>
using namespace std;
main()
{
char name[20];
float cost;
ofstream obj("data.txt");
cout<<"Enter name";
cin>>name;
cout<<"Enter cost";
cin>>cost;
obj<<name<<endl;
obj<<cost<<endl;
obj.close();
ifstream ob("data.txt");
ob>>name;
ob>>cost;
cout<<name;
cout<<cost;
ob.close();
}
OUTPUT
Enter name
RAM
Enter cost
20000
RAM
20000
R138,89
Get access to the full document:

100% satisfaction guarantee
Immediately available after payment
Both online and in PDF
No strings attached

Get to know the seller
Seller avatar
monika19sharmaa

Get to know the seller

Seller avatar
monika19sharmaa A private College
Follow You need to be logged in order to follow users or courses
Sold
0
Member since
3 months
Number of followers
0
Documents
20
Last sold
-

0,0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their exams and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can immediately select a different document that better matches what you need.

Pay how you prefer, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card or EFT and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Frequently asked questions