C++ class program to illestrate File operations

AIM
Write a program to illustrate the write() member function which are usually used for transfer of data blocks to the file.

ALGORITHM

1. Start the process
2. Invoke the class
a. Create two inline member functions .ie, getdata() and dispdata()
i. getdata() for input
ii. dispdata() for display
3. Open the file in fstream mode
4. Write the data in to the file
5. Slose the file
6. Stop the process

PROGRAM

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
class student
{
private:
int rno;
char name[10];
float fees;
public:
void getdata()
{
cout<<"roll number";
cin>>rno;
cout<<endl;
cout<<"enter name:";
cin>>name;
cout<<endl<<"enter fees:";
cin>>fees;
}
void dispdata()
{
cout<<"Roll number"<<rno<<endl;
cout<<"Name"<<name<<endl;
cout<<"Fees"<<fees;
}
};
 
void main()
{
student s1;
clrscr();
ofstream stdfile("c:\\std.txt");
//fstream stdfile;
//stdfile.open("c:\\std.txt",ios::out|ios::in);  //open file for output
char wish;
//writing to the file
do
{
s1.getdata();
stdfile.write((char*)&s1,sizeof(student));
cout<<"continue ? y/n";
cin>>wish;
}
while(wish=='y'||wish=='Y');
stdfile.close();   //close the file
 
getch();
}

OUTPUT

Roll number 121
Enter name Jacob
Enter fees 10000

Editorial Team
Editorial Team

We are a group of young techies trying to provide the best study material for all Electronic and Computer science students. We are publishing Microcontroller projects, Basic Electronics, Digital Electronics, Computer projects and also c/c++, java programs.

3 thoughts on “C++ class program to illestrate File operations

  1. Write more, thats all I have to say. Literally, it seems as though you
    relied on the video to make your point. You obviously know
    what youre talking about, why throw away your intelligence on just posting videos to your weblog when you
    could be giving us something informative to read?

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in