Sei sulla pagina 1di 27

//Program to ENTER RECORDS using Data file handling.

#include<fstream.h>
#include<conio.h>
void main()
{
clrscr();
ofstream filout;
filout.open("marks.nik",ios::out);
char ans='y';
int rollno;
float marks;
clrscr();
while(ans=='y' || ans=='Y')
{
cout<<"\n ENTER ROLLNO. : ";
cin>>rollno;
cout<<"\n ENTER MARKS : ";
cin>>marks;
filout<<rollno<<'\n'<<marks<<'\n';
cout<<"\n WANT TO ENTER MORE RECORDS? (y/n)...";
cin>>ans;
}
filout.close();
}
//Program to ENTER 5 RECORDS Data file handling.
#include<fstream.h>
#include<conio.h>
void main()
{
clrscr();
ofstream fout("student.nik");
char name[30],ch;
float marks=0.0;
for(int i=0;i<5;i++)
{
cout<<"STUDENT "<<i+1<<" : \t NAME : ";
cin.get(name,30);
cout<<"\t\t MARKS : ";
cin>>marks;
cin.get(ch);
fout<<name<<'\n'<<marks<<'\n';
}
fout.close();
ifstream fin("student.nik");
fin.seekg(0);
cout<<"\n";
for(i=0;i<5;i++)
{
fin.get(name,30);
fin.get(ch);
cout<<" STUDENT NAME : "<<name;
cout<<"\t MARKS : "<<marks<<"\n";
}
fin.close();
getch(); }
//Program to create a single file and then display its

contents
#include<fstream.h>
#include<conio.h>
void main()
{
clrscr();
ofstream filout;
filout.open("stunames.nik");
filout<<"nitish karnatak\n"<<"nikki\n"<<"niks\n";
filout.close();
filout.open("stumarks.nik");
filout<<"99\n"<<"98\n"<<"97\n";
filout.close();
char line[80];
ifstream filin;
filin.open("stunames.nik");
cout<<"\n THE CONTENTS OF stunames.nik FILE ARE GIVEN BELOW :\n";
filin.getline(line,80);
cout<<line<<"\n";
filin.getline(line,80);
cout<<line<<"\n";
filin.getline(line,80);
cout<<line<<"\n";
filin.close();
filin.open("stumarks.nik");
cout<<"\n THE CONTENTS OF stumarks.nik FILE ARE GIVEN BELOW \n";
filin.getline(line,80);
cout<<line<<"\n";
filin.getline(line,80);
cout<<line<<"\n";
filin.getline(line,80);
cout<<line<<"\n";
filin.close();
getch();
}
//Program to use multiple files in succession.
#include<fstream.h>
#include<conio.h>
void main()
{
clrscr();
ofstream filout;
filout.open("stunames.nik");
filout<<"nitish karnatak\n"<<"nikki\n"<<"niks\n";
filout.close();
filout.open("stumarks.nik");
filout<<"99\n"<<"98\n"<<"97\n";
filout.close();
ifstream filin1, filin2;
filin1.open("stunames.nik");
filin2.open("stumarks.nik");
char line[80];
cout<<" \n THE CONTENTS OF stunames AND stumarks ARE GIVEN BELOW : ";
filin1.getline(line,80);
cout<<line<<"\n";
filin2.getline(line,80);
cout<<line<<"\n";
filin1.getline(line,80);
cout<<line<<"\n";
filin2.getline(line,80); cout<<line<<"\n";
filin1.getline(line,80); cout<<line<<"\n";
filin2.getline(line,80); cout<<line<<"\n";
filin1.close(); filin2.close();
getch();
}

//Program to use multiple files simultaneously


#include<fstream.h>
#include<conio.h>
void main()
{
ofstream fout;
fout.open("marks.nik", ios::app);
char ans='y';
int rollno;
float marks;
clrscr();
while(ans=='y' || ans=='Y')
{
cout<<"\n ENTER ROLLNO. : ";
cin>>rollno;
cout<<"\n ENTER MARKS : ";
cin>>marks;
fout<<rollno<<'\n'<<marks<<'\n';
cout<<"\n WANT TO ENTER MORE RECORDS? (y/n)...";
cin>>ans;
}
fout.close();
}
//Program to display contents of a file using get() function.
#include<fstream.h>
#include<conio.h>
#include<process.h>
void main()
{
clrscr();
char ch;
ifstream fin;
fin.open("master.nik", ios::in | ios::binary);
if(!fin)
{
cout<<"\n CANNOT OPEN THE FILE!!";
cout<<"\n PRESS ANY KEY TO EXIT...";
getch();
exit(1);
}
while(fin)
{
fin.get(ch);
cout<<ch;
}
fin.close();
getch();
}
//Program to create a file using put().
#include<fstream.h>
#include<conio.h>
#include<process.h>
void main()
{
clrscr();
ofstream fout;
fout.open("aschars.nik",ios::app);
if(!fout)
{
cout<<" \n THE FILE CANNOT BE OPENED !!";
cout<<"\n PRESS ANY KEY TO EXIT...";
getch();
exit(0);
}
char ch;
int line=0;
for(int i=0;i<256;i++)
{
if(i!=26)
{
fout.put(((char) i));
}
}
fout.close();
ifstream fin;
fin.open("aschar" ,ios::in);
fin.seekg(0);
for(i=0;i<256;i++)
{
if(i!=26)
{
fin.get(ch);
cout<<" "<<i<<" = ";
cout.put((char)(i));
}
if(!(i%6))
{
cout<<endl;
line++;
}
if(line>22)
{
getch();
line=0;
}
}
getch();

}
//Program to write & read a structure using write() & read()

function using a binary file.


#include<fstream.h>
#include<conio.h>
#include<string.h>
#include<process.h>

struct customer
{
char name[40];
float balance;
};

void main()
{
clrscr();
customer savac;
strcpy(savac.name, " Sandeep Arora ");
savac.balance=21310.75;
ofstream fout;
fout.open("saving.nik" , ios:: out | ios::binary);
if(!fout)
{
cout<<"\n FILE CANNOT BE OPENED ....ABORTING !!";
getch();
exit(0);
}

fout.write((char *) &savac, sizeof(customer));


fout.close();
ifstream fin;
fin.open("saving.nik", ios:: in | ios:: binary);
fin.read((char *) &savac, sizeof(customer));
cout<<savac.name;
cout<<" HAS A BALANCE Rs."<<savac.balance<<endl;
fin.close();

getch();
}
//Program for reading & writing class objects
#include<fstream.h>
#include<conio.h>
#include<process.h>

const int size=3;


class student
{
char name[30];
char grade;
float marks;
public :
void getdata(void);
void display(void);
};
void student :: getdata(void)
{
char ch;
cin.get(ch);
cout<<"ENTER NAME : ";
cin.getline(name,40);
cout<<"\n ENTER GRADE : ";
cin>>grade;
cout<<"\n ENTER MARKS : ";
cin>>marks;
cout<<"\n";
}
void student:: display(void)
{
cout<<" NAME : "<<name<<"\t"<<" GRADE : "<<grade<<"\t MARKS :"<<marks<<"\t\n";
}
void main()
{
clrscr();
student arts[size];
fstream filin;
filin.open("stu.nik",ios::in| ios::out);
if(!filin)
{
cout<<"\n CANNOT OPEN FILE...ABORTING!!! ";
getch();
exit(0);
}
cout<<"\n ENTER DETAILS FOR" <<size<<" STUDENTS :\n ";
for(int i=0;i<size;i++)
{
arts[i].getdata();
filin.write((char*) & arts[i],sizeof(arts[i]));
}
filin.seekg(0);
cout<<"\n THE CONTENTS OF stu.nik ARE SHOWN BELOW..\n";
for(i=0;i<size;i++)
{
filin.read((char*) & arts[i],sizeof(arts[i]));
arts[i].display();
}
filin.close();
getch();
}
//Program to modify data in a given file
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<process.h>
int count=0;
class student
{
char name[40];
char grade;
float marks;
public :
void getdata(void);
void display(void);
void mod_data();
};
void student :: getdata(void)
{
char ch;
cin.get(ch);
clrscr();
gotoxy(15,10);
cout<<"\n ADD STUDENT DATA \n";
gotoxy(17,12); cout<< "RECORD # "<<(++count)<<endl;
gotoxy(1,14);
for(int i=0; i<40;i++)
name[i]=' ';
grade=' ';
marks=0.0;
cout<<"ENTER NAME : ";
cin.getline(name,30);
cout<<"\nENTER GRADE :" ;
cin>>grade;
cout<<"\n ENTER MARKS : ";
cin>>marks;
cout<<"\n";
}
void student :: display(void)
{
clrscr();
gotoxy(15,10);
cout<<" STUDENT DETAILS \n";
gotoxy(1,12);
cout<<"NAME : "<<name<<"\t GRADE : "<<grade<<"\t MARKS : "<<marks<<"\n";
}
void student :: mod_data(void)
{
char nm[40], gr= ' ';
float mk=-1;
clrscr();
gotoxy(15,8);
cout<<"\n MODIFY STUDENT DATA \n"<<endl;
char ch=cin.get();
cout<<ch;
gotoxy(17,10);
cout<<"\n CURRENT DETAILS\n";
gotoxy(17,11);
cout<<"_______________\n\n";
cout<<"NAME : "<<name<<"\t GRADE : "<<grade<<"\t MARKS : "<<marks<<"\n";
gotoxy(17,16);
cout<<"\n ENTER NEW DETAILS\n ";
gotoxy(17,17);
cout<<"_______________\n\n";
gotoxy(10,24);
cout<<"\n\n IF YOU WANT TO RETAIN OLD NAME OR GRADE , JUST PRESS
ENTER.";
gotoxy(50,17);
cout<<endl;
cout<<"NAME :"; cin.getline(nm,40);
cout<<"GRADE :"; gr=getche();
cout<<"MARKS :";cin>>mk;
if(strlen(nm)!=0)strcpy(name,nm);
if((gr!=13) && (gr!=32)) grade=gr;
gotoxy(10,24);
clreol();
}

void main()
{
clrscr();
student stud;
fstream filin;
filin.open(" stumast.dat",ios::in | ios::out| ios :: binary);
if(!filin)
{
cout<<"CANNOT OPEN FILE!!\n";
getch();
exit(0);
}
int choice, mrec=0,offset=0;
char ans;
do
{
clrscr();
cout<<"\n\n\n\t\t MAIN MENU\n";
cout<<"\t\t________________\n";
cout<<"\n1.ADD RECORD.";
cout<<"\n2.MODIFY RECORD.";
cout<<"\n3.DISPLAY RECORD.";
cout<<"\n4.EXIT.";
cout<<"\n ENTER YOUR CHOICE...(1-4)";
cin>>choice;
switch(choice)
{
case 1 :
stud.getdata();
mrec=count;
offset=((mrec-1)*sizeof(student));
filin.seekp(offset,ios::beg);
filin.write((char*)&stud,sizeof(student));
break;
case 2:
if(!count)
{
cout<<"NO RECORD HAS BEEN ADDED.\n";
cout<<"PLEASE RUN OPTION 1 FIRST OF ALL. ";
gotoxy(10,23);
cout<<"PRESS ANY KEY TO CONTINUE...";
getch();
break;
}
cout<<"\n\n MODIFY WHICH RECORD ??#";
cin>>mrec;
if(mrec>count)
{
cout<<"\n\n\n ONLY "<<count<<" RECORDS HAVE BEEN ADDED.\n";
cout<<"INVALID RECORD NUMBER..\n";
gotoxy(15,23);
cout<<"PRESS ANY KEY TO CONTINUE...";
getch();
break;
}
else
{
offset=(mrec-1)*sizeof(student);
filin.seekg(offset,ios::beg);
filin.read((char*)&stud,sizeof(student));
stud.display();
cout<<"MODIFY THIS RECORD ? (y/n)...";
cin>>ans;
if(ans=='y' || ans=='Y')
{
cout<<"ENTER NEW DETAILS\n";
stud.mod_data();
filin.seekp(offset,ios::beg);
filin.write((char*)&stud,sizeof(student));
cout<<"\n RECORD MODIFIED\n";
gotoxy(30,24);
cout<<"\n PRESS A KEY TO CONTINUE..";
getch();
}
break;
}
case 3:
if(!count)
{
cout<<"NO RECORD HAS BEEN ADDED.\n";
cout<<"PLEASE RUN OPTION 1 FIRST OF ALL. ";
gotoxy(10,23);
cout<<"PRESS ANY KEY TO CONTINUE...";
getch();
break;
}
cout<<"\n\n DISPLAY WHICH RECORD ??#";
cin>>mrec;
if(mrec>count)
{
cout<<"\n\n\n ONLY "<<count<<" RECORDS HAVE BEEN ADDED.\n";
cout<<"INVALID RECORD NUMBER..\n";
gotoxy(15,23);
cout<<"PRESS ANY KEY TO CONTINUE...";
getch();
break;
}
else
{
offset=(mrec-1)*sizeof(student);
filin.seekg(offset,ios::beg);
filin.read((char*)&stud,sizeof(student));
stud.display();
gotoxy(10,23);
cout<<"\n PRESS ANY KEY TO CONTINUE..";
getch();
}
break;
case 4: break;
default : cout<<"WRONG CHOICE !! VALID CHOICES ARE 1-4.\n";
break;
}
}while(choice>=1 && choice<=3);
filin.close();
getch();
}
//A FILE NAMED MARKS.DAT ALREADY STORES SOME STUDENTS DETAILS.
WRITE A PROGRAM THAT READS MORE DETAILS AND APPENDS THEM TO THIS
FILE .

#include<fstream.h>
#include<iostream.h>
#include<conio.h>
void main()

{
clrscr();
ifstream filin1,filin2;
filin1.open("stunames");
filin2.open("stumarks");
char line[80];
cout<<" \t The contents of stunames and stumarks are";
filin1.getline(line,80);
cout<<line<<"\n" ;
filin2.getline(line,80);
cout<<line<<"\n" ;
filin1.getline(line,80);
cout<<line<<"\n" ;
filin2.getline(line,80);
cout<<line<<"\n" ;
filin1.close();
filin2.close();
getch();
}
// PROGRAM TO APPEND DATA IN FILE
#include<fstream.h>
class stu
{
int rollno;
char name[25] ;
char clas[4];
float marks ;
char grade;
public:
void getdata()
{
cout<<" roll no";cin>>rollno;
cout<<" Name ";cin>>name ;
cout<<" class";cin>>clas;
cout<<"marks";cin>>marks;
if(marks>=75 )grade='A';
else if (marks>=60)grade='B';
else if (marks>=50)grade='C' ;
else if (marks>=40)grade='D';
else grade ='F';
}
void putdata()
{
cout<<name<<", rollno "<<rollno<<"has"<<marks<<"% marks and"
<<grade <<"grade"<< endl;
}
int getrno() //accessor function
{
return rollno;
}
}
s1;
void main()
{
ofstream fo("stu.dat",ios::app); char ans='y' ;
while (ans=='y')
{
s1.getdata();
fo.write((char*) & s1,sizeof(s1));
cout<<"record added to file ";
cout<<"want to enter more records";
cin>>ans;
}
fo.close();
}
//GET ROLLNOS & MARKS OF STUDENTS OF A CLASS AND STORE
THESE INTO A FILE CALLED MARKS.DAT
#include<fstream.h>
#include<iostream.h>
#include<conio.h>
void main()
{
ofstream filout;
filout.open(" marks.dat",ios::out);
char ans='y';
int rollno,marks;
while( ans=='y')
{
cout<<"\n enter roll_no ";
cin>>rollno;
cout<<" \n enter marks";
cin>>marks;
filout<<rollno<<"\n"<<marks;
cout<<" want to enter more records( y/n ) ";
cin>>ans;
}
filout.close();
}
// PROGRAM TO DELETE A RECORD FROM FILE
#include<iostream.h>
#include<fstream.h>
#include<string.h>
#include<stdio.h>
class stu
{
int rollno;
char name[25];
char clas[4];
float marks;
char grade;
public :
if(marks>=60) grade='B';
else if(marks>=50) grade='C';
else if(marks>=40) grade='D';
else grade ='F';
}
void putdata()
{
cout<<"rollno"<<rollno<<"\tname:"<<name<<"\tgrade:"<<grade<<endl;
}
int getrno()

{
return rollno;
}
}s1,stud;
void main()
{
ifstream fio("stu.dat",ios::in);
ofstream file("temp.dat",ios::out);
int rno;
char found='f',confirm='n';
cout<<" enter rollno of student whose record is to be deleted ";
cin>>rno;
while (!fio.eof() )
{ fio.read((char*)&s1,sizeof(s1));
if (s1.getrno()==rno)
{ s1.putdata();
found='t';
cout<<"are you sure ,you want to delete this record";
cin>>confirm;
if(confirm=='n')
file.write((char*)&s1,sizeof(s1) ); }
else
file.write((char*)&s1,sizeof(s1));
}
if (found=='f')
cout<<"record not found";
fio.close();
file.close();
remove("stu.dat") ;
rename("temp.dat","stu.dat");
fio.open("stu.dat",ios::in);
cout<<"now the file contains \n";
while(!fio.eof())
{ fio.read((char*)&stud,sizeof(stud));
if(fio.eof())
stud.putdata();
}
fio.close(); }

Potrebbero piacerti anche