The Code given below can be used in TURBO C++ Compilers: -
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <string.h>
#include <fstream.h>
#include <stdlib.h>
class Patient
{
public:
struct patient
{
int regNo;
char name[15];
float fees;
} Pa;
Patient()
{
Pa.regNo = 0;
strcpy(Pa.name, "NULL");
Pa.fees = 0.0;
} // constructor ends
void getdata()
{
cout << "Enter data\n";
cout << "Enter registration no. :";
cin >> Pa.regNo;
cout << "Enter name :";
cin >> Pa.name;
cout << "Enter fees : ";
cin >> Pa.fees;
} // getdata() ends
void putdata()
{
ifstream fin;
fin.open("PATIENT.DAT", ios::in | ios::binary);
cout << "Registration no= " << Pa.regNo << "\n";
cout << "Patient Name = " << Pa.name << "\n";
cout << "Fees : " << Pa.fees << "\n";
} // putdata() ends
} P;
void append()
{
ofstream fout;
fout.open("PATIENT.DAT", ios::out | ios::binary);
if (!fout)
{
cout << "File does not exist!\n";
exit(0);
}
int i = 1;
while (i <= 3)
{
P.getdata(); // class function
fout.write((char *)&P, sizeof(P));
i++;
} // while loop ends
fout.close();
} // append() ends
void displayrec(int rno)
{
ifstream fin;
fin.open("PATIENT.DAT", ios::in | ios::binary);
if (!fin)
{
cout << "File doesn't exist.\n";
exit(0);
}
while (fin)
{
fin.read((char *)&P, sizeof(P));
if (fin.eof())
{
break;
}
if (P.Pa.regNo == rno)
{
P.putdata();
break;
}
else
cout << "Record not found.\n";
} // while loop ends
fin.close();
} // displayrec() ends
int main()
{
clrscr();
char ch1;
char ch = 'Y';
while (ch == 'Y' || ch == 'y')
{
cout << "Enter choice: \n";
cout << "a.To enter 3 records in the file. \n";
cout << "b.To display the record of a patient, from the file of a given the regNo\n";
cout << "c.To display all the records stored in the file.. \n";
cout << "d. Exit.\n";
cin >> ch1;
switch (ch1)
{
case 'a':
{
append();
break;
}
case 'b':
{
int rno;
cout << "Enter regNo of record to be printed: ";
cin >> rno;
displayrec(rno);
break;
}
case 'c':
{
ifstream fin;
fin.open("PATIENT.DAT", ios::in | ios::binary);
if (!fin)
break;
while (!fin.eof())
{
fin.read((char *)&P, sizeof(P));
P.putdata();
cout << "\n\n";
}
fin.close();
break;
}
case 'd':
{
ch = 'N';
exit(0);
} // exit
} // switch ends
cout << "Would you like to continue? [Y/N]\n";
cin >> ch;
} // while ends
getch();
return 0;
} // main() ends;
The Code given below can be used in gcc/g++ Compilers: -
#include <iostream>
#include <string.h>
#include <fstream.h>
using namespace std;
class Patient
{
public:
struct patient
{
int regNo;
char name[15];
float fees;
} Pa;
Patient()
{
Pa.regNo = 0;
strcpy(Pa.name, "NULL");
Pa.fees = 0.0;
} // constructor ends
void getdata()
{
cout << "Enter data\n";
cout << "Enter registration no. :";
cin >> Pa.regNo;
cout << "Enter name :";
cin >> Pa.name;
cout << "Enter fees : ";
cin >> Pa.fees;
} // getdata() ends
void putdata()
{
ifstream fin;
fin.open("PATIENT.DAT", ios::in | ios::binary);
cout << "Registration no= " << Pa.regNo << "\n";
cout << "Patient Name = " << Pa.name << "\n";
cout << "Fees : " << Pa.fees << "\n";
} // putdata() ends
} P;
void append()
{
ofstream fout;
fout.open("PATIENT.DAT", ios::out | ios::binary);
if (!fout)
{
cout << "File does not exist!\n";
exit(0);
}
int i = 1;
while (i <= 3)
{
P.getdata(); // class function
fout.write((char *)&P, sizeof(P));
i++;
} // while loop ends
fout.close();
} // append() ends
void displayrec(int rno)
{
ifstream fin;
fin.open("PATIENT.DAT", ios::in | ios::binary);
if (!fin)
{
cout << "File doesn't exist.\n";
exit(0);
}
while (fin)
{
fin.read((char *)&P, sizeof(P));
if (fin.eof())
break;
if (P.Pa.regNo == rno)
{
P.putdata();
break;
}
else
{
cout <<"Record not found.\n";
}
} // while loop ends
fin.close();
} // displayrec() ends
int main()
{
char ch1;
char ch = 'Y';
while (ch == 'Y' || ch == 'y')
{
cout << "Enter choice: \n";
cout << "a.To enter 3 records in the file. \n";
cout << "b.To display the record of a patient, from the file of a given the regNo\n";
cout << "c.To display all the records stored in the file.. \n";
cout << "d. Exit.\n";
cin >> ch1;
switch (ch1)
{
case 'a':
append();
break;
case 'b':
int rno;
cout << "Enter regNo of record to be printed: ";
cin >> rno;
displayrec(rno);
break;
case 'c':
{
ifstream fin;
fin.open("PATIENT.DAT", ios::in | ios::binary);
if (!fin)
break;
while (!fin.eof())
{
fin.read((char *)&P, sizeof(P));
P.putdata();
cout << "\n\n";
}
fin.close();
break;
}
case 'd':
ch = 'N';
exit(0); // exit
} // switch ends
cout << "Would you like to continue? [Y/N]\n";
cin >> ch;
} // while ends
return 0;
} // main() ends;<<
#ENJOY CODING
Post a Comment
FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP