Thursday, 4 May 2017

AIM:Write a C++ program that creates an output file, writes information to it, closes the file and open it again as an input file and read the information from the file





CODING:
#include<fstream.h>
#include<conio.h>
class student
{
   char name[20];
   int roll;
   public:
     void getdata()
     {  cout<<"Enter Name:"<<endl;
            cin>>name;
            cout<<"Enter roll No:"<<endl;
            cin>>roll;
     }
       void showdata()
       { cout<<"Name is:"<<name<<endl;
             cout<<"Roll No is"<<roll<<endl;
            }
};
main()
{  clrscr();
   student s[2];

   ofstream of;
   of.open("student.txt");
   for(int i=0;i<2;i++)
   {
     s[i].getdata();
     of.write((char *)&s[i],sizeof(s[i]));
     of.close();
   }
   ifstream fin;
   fin.open("student.txt");
   for(i=0;i<2;i++)
   {
     fin.read((char *)&s[i],sizeof(s[i]));
     s[i].showdata();
     fin.close();
   }
   getch();
   return 0;

}

No comments:

Post a Comment

About Me

Hi, I am Prof. Amol Zade, working in the field of teaching and research from last more than 10 years. My area of interests are Artificial Intelligence, Wireless Networking, Algorithms. I have 16 International paper publications along with 4 International Conference; also 3 Books published. published on Object Oriented Programming with open source approach.