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
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