Thursday, November 3, 2016

FILE HANDLING :)

Tags

C++ Program to Illustrate the Concept of File Handling.

#include <iostream>
#include <fstream> //Header file for File Operations
using namespace std;
int main()
{
    int sal,age;
    char name[20];
    cout<<"Enter Name: ";
    cin>>name;
    cout<<"Enter Age: ";
    cin>>age;
    cout<<"Enter Salary: ";
    cin>>sal;
    cout<<"\n\nWriting to File..\n";

    ofstream fout;
    fout.open("employee.dat"); //Opening file "employee.dat"  for writing using open() Function
    fout<<name<<"\n"; //Writing Data to File
    fout<<age<<"\n";
    fout<<sal<<"\n\n";
    cout<<"\nClosing File..\n\n";
    fout.close(); //Closing File

    cout<<"Reading from File..\n\n";
    ifstream fin; //Opening file for Reading using open() Function
    fin>>name>>age>>sal; //Reading Data from File
    cout<<"Name: "<<name;
    cout<<"\nAge: "<<age;
    cout<<"\nSalary: "<<sal;
    cout<<"\n\nClosing File..\n";
    fin.close();
    return 0;
}



employee.dat File