Professional Documents
Culture Documents
assignment-2
assignment-2
assignment-2
ASSIGNMENT # 2
File handling:
What is file handling?
File Handling in C++ is used to store, write or read the data permanently in computer’s hard
disk. By using file handling we can store data is secondary data.
For file handling we need another C++ library called fstream, which defines another three new data
type.
Steps:
1. Name a file.
2. Open a file.
3. Read / write data on file.
4. Close a file.
Function used:
1
SYED MOIZ ALI CMS:32798
Code:
For example:
Ofstream writer(“Moiz.txt”);
Main code:
#include<iostream>
#include<conio.h>
using namespace std;
#include<fstream>
void main()
{
ofstream writer("Moiz.txt");
writer<<"this is my fir";
getch();
}
After execution:
2
SYED MOIZ ALI CMS:32798
For example:
ifstream reader("Moiz.txt");
Main code:
#include<iostream>
#include<conio.h>
#include<fstream>
using namespace std;
void main()
{
int c;
ifstream reader("Moiz.txt");
int a, b;
reader>>a>>b;
cout<<a+b;
getch();
3
SYED MOIZ ALI CMS:32798
}