Program to perform read and write operations on a file.

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class a{
public:
void writeFile()
{
ofstream myfile ("example.txt");
  if (myfile.is_open())
  {
    myfile << "This is iView Blogger.\n";
    myfile << "iView-The Way i-View!!.\n";
    myfile.close();
    cout<<"File Saved!"<<endl;
  }
  else
    cout << "Unable to open file";
}
void readFile()
{
string line;
  ifstream myfile ("example.txt");
  if (myfile.is_open())
  {
    while ( getline (myfile,line) )
    {
      cout << line << '\n';
    }
    myfile.close();
  }
  else
  cout << "Unable to open file";
}
void Quit ()
{
    cout << "\tThanks For iView-ing!!\n\t\tExiting" << endl;
    cout<<"\n/*\nName-Sanjampreet Singh\nRoll Number-1507967\n*/";
    exit (-1);
}


};
class start:public a
{
public:
void startq(){
int opt;
cout<<"\n\n1.Write"<<endl<<"2.Read"<<endl<<"3.Exit"<<endl<<"Enter your choice : ";
cin>>opt;
switch(opt)
{
case 1:
writeFile();
startq();

case 2:
readFile();
startq();

case 3:
Quit();

default:
cout<<"error"<<endl;
startq();
}}
};
int main()
{
start ab;
ab.startq();
}

Output:


Comments

Popular posts from this blog

To create an html page with frames and frameset

1. Write a program to insert a new element at end as well as at a given position in an array in Java

1. To familiarize with network devices like switch, hub, routers and bridges in LaTex