क्योंकि यह program थोड़ा बड़ा होने वाला है इसलिए हरेक task को paragraph के रूप में बांटा गया है ताकि इसे आसानी से समझा जा सके।
तो चलिए शुरू करते हैं।
File handling Program in C++
insert record
Display record
Delete record
Search record
Modify record
Properties
Exit
यह block header files का है इसमें comments करके हमने उनके pre -defined function को बताया है कि कोनसा सा function किस header file से लिया गया है।
// header file include
#include<iostream.h>
#include<conio.h>
#include<stdio.h> rename and remove
#include<stdlib.h> // exit()
#include<iomanip.h> // setw()
#include<fstream.h> // file handling
//data member and member function declaration
creating a class name is student
program में एक class , जिसका नाम student है, declare किया गया है। इस class में public में , कई user defined function declare किये गए हैं। साथ ही constructor भी declare किया गया है-
class student
{
int roll;char sname[29];
char fname[20],mname[20];
public:
// function declaration in public mode
student(void); // constructor declaration for intro wizard
~student(void){ } // destroy object initialized by constructor
void input_record(void); // write record into file
void putdata_record(void); // display all record
void putdata_form(void); // create a template to displaying record on monitor
void search_record(); // search a specific record
void total(); // properties of program
void modify_form(void); // create a template to modifying record
void modify_record(); // modify record into file
void delete_record(); // delete spacific record
}obj; // constructor called automatically
यह program का introduction है जो program के starting में display होता है। system (pause) function, screen को hold करता है।
student::student(void)
{
clrscr();
cout<<"/t/t/tThis program clear some concept of file handling/n";
cout<<"/t/t";
system("pause");
}
To insert record in the file
input_record() function used to write a file which will be call by other way,
program में database नाम से file create की गयी है। function input_record () से हम इस file में data को write करते हैं। ध्यान रहे यहाँ पर हमने data को write करने का जो तरीका इस्तेमाल किया गया है उससे file में data binary format में store होगा-
//get record by end user
void student::input_record(void)
{
clrscr();
ofstream fout;
cout<<"INSERT RECORD/n";
fout.open("database",ios::app|ios::binary);
cout<<"Enter roll no: ";
cin>>roll;
cout<<"Enter stuname: ";
gets(sname);
cout<<"Enter father name: ";
gets(fname);
cout<<"Enter mother name: ";
gets(mname);
fout.seekp(0,ios::beg); //go to start
fout.write((char*)&obj,sizeof(obj));
fout.close();
cout<<"/n/n/tRecord saved successfully...";
create a form for display records into monitor screen
putdata_form() file में stored data को screen पर display करने का एक तरीका है इसमें बताया गया है कि हम data को screen पर किस प्रकार display कराना चाहते हैं। ध्यान दें इस function से हम data को fetch नहीं कर रहे हैं। सिर्फ display करा रहे हैं।
void student::putdata_form(void)
{
cout<<roll<<"/t"<<sname<<"/t"<<fname<<"/t"<<mname<<endl;
}
To display record from in the file
putdata_record () से हम file से data को fetch कर रहे हैं। जैसे ही यह function call होता है यह data को screen पर display कराने के लिए दूसरे function putdata_form () को कॉल करेगा-
//display all record into monitor screen
void student::putdata_record(void)
{
clrscr();
ifstream fin;
cout<<"SHOW ALL RECORD/n";
fin.open("database",ios::app|ios::binary);
fin.seekg(0,ios::beg); //goto begining or start
while(fin.read((char*)&obj,sizeof(student)))
{
putdata_form();
}
fin.close();
getch(); //hold screen
}
In above codes
आप चाहे तो यहाँ पर दो function declare ना करके केवल एक ही function से data को display और access करा सकते हैं परन्तु ऐसा करने से इस function की readability घट सकती है। इसलिए इसे यहाँ पर दो भागो/function में बांटा गया है।
To Delete Record From a file
rename and remove function, both are defined in stdio.h header file.
इसके लिए हम दो function को इस्तेमाल में लाते हैं जो कि file handling से सबंधित नहीं रखते-
for rename file
rename (old-file-name, new-file-name);
for remove file
remove (filename);
for more check file handling concept in C++
file में जहाँ इतने student records store हों। उनमे से केवल किसी specific record को ही delete करना है। इसके लिए उस data की एक unique id होनी चाहिए। जैसे real world में एक जैसे mobile number दो प्रकार के नहीं हो सकते।
यहाँ पर हमने student का roll no भी store किया है जो की एक unique id है। ऐसे में हम किसी specific student के record को उसके roll no से एक्सेस कर सकते हैं –
void student::delete_record()
{
int x,true=0;
clrscr();
ofstream fout;
ifstream fin;
cout<<"DETELE RECORD/n";
cout<<"Enter student roll no: ";
cin>>x;
int r = x;
fin.open("database",ios::binary);
fout.open("temp",ios::binary);
while(fin.read((char*)&obj,sizeof(obj)))
{
if(x==r) // accessing record using their roll no
{
if(x!=roll) // which record not matched write into temp file
{
fout.write((char*)&obj,sizeof(student));
true=1;
}
}
}
fout.close();
fin.close();
remove("database"); //remove file
rename("temp","database"); //rename file
if(true)
cout<<"record deleted...";
if(true!=1)
cout<<"record not found.....";
}
इस statement पर ध्यान दें-
if(x!=roll)
अर्थात, हम केवल एक record को छोड़कर (जिसे हम delete करना चाहते हैं ) बाकी सभी records को किसी दूसरी file(temp) में write कराते हैं और इस old file(database) को delete कर देते हैं तथा इस new file(temp) को rename करते हैं।
इस प्रकार हम यह task perform करते हैं। search operation में भी roll no से किसी specific record को access किया जायेगा।
To search a specific record in the file
Here we will search student record using their roll no as a unique id
जैसे की पहले ही बताया गया है इसके लिए हम roll no से पता लगाएंगे की record file में available है या नहीं-
void student::search_record()
{
clrscr();
int x;
ifstream fin;
cout<<"SEARCH RECORD/n";
cout<<"Enter student roll no: ";
cin>>x;
fin.open("database",ios::app|ios::binary);
while(fin.read((char*)&obj,sizeof(obj)))
{
if(x==roll) // if record found display
{
putdata_form();
}
}
fin.close();
getch();
}
above codes will fetch the record and will call a function putdata_form() which will display.
अगर record file available है तो putdata_form() से हम उस record को screen पर display कराते हैं।
create a form to modify old record
it is simple form to display records which we want to modify.
putdata_form() की तरह ही modify_form () है यह modified किये हुए data को display करता है
void student::modify_form(void)
{
cout<<"Enter new roll no: ";
cin>>roll;
cout<<"Enter new stuname: ";
gets(sname);
cout<<"Enter new father name: ";
gets(fname);
cout<<"Enter new mother name: ";
gets(mname);
cout<<"/n/ntRecord updated successfully...";
}
To modify a specific record in the file
यहाँ पर किसी record को modify करने का syntax नीचे दिया गया है –
int variable=(object-1)*sizeof(class-name/class-object);
as we know unique id will be student’s roll-no. so,
जिस record को modify करना है उसे हम उसके roll -no से access करते हैं और putdata_form () से display कराते हैं-
// modify specific student record using their roll no
void student::modify_record()
{
clrscr();
int x; char true=0;
fstream file;
cout<<"MODIFY RECORD/n" ;
cout<<"Enter roll no: ";
cin>>x;
file.open("database",ios::in|ios::out|ios::binary);
while(file.read((char*)&obj,sizeof(student)))
{
if(x==roll)
{
cout<<"/nOld record are n";
putdata_form();
cout<<"/n/nEnter new record/n/n";
modify_form();
int loc=(-1)*sizeof(obj);
file.seekp(loc,ios::cur); // give current found location into file
file.write((char*)&obj,sizeof(student));
true=1;
}
}
file.close();
if(!true) //true!=1
cout<<"record not found.....";
getch();
}
To calculate Total record in the file
file handling में कुछ manipulators है जिनका use कई different task के लिए किया जाता है उसमे से किसी एक का उदाहरण नीचे दिया गया है। जिससे हम ज्ञात करते हैं कि किसी file में कितना record है या file का size कितना है। यह एक तरह से file की property show करता है। इसलिए इसे एक अलग option की तरह इस्तेमाल किया गया है।
// show properties of program like total record and size of file
void student::total()
{
clrscr();
cout<<"ABOUT PROGRAM/n";
int total_rec,st;
ifstream fin;
fin.open("database",ios::in|ios::out|ios::binary);
/*******using manipulators*********/
fin.seekg(0,ios::end); // goto end of file
total_rec=fin.tellg()/sizeof(student);
st = fin.tellg();
cout<<"/n/ntTotal record in file of student are: "<<total_rec;
cout<<"/ntMemory of database is: "<<st<<" byte";
_setcursortype(_NOCURSOR); // disable mouse cursor
getch();
}
main program start here
यह इस पुरे program का starting point है यह एक साधारण program की तरह ही है –
void main()
{
char ch;
do
{
clrscr();
gotoxy(40,5);// print message a specific location into monitor
cout<<"SELECTION MENUn";
gotoxy(40,7);
cout<<"1.INSERT RECORDn";
gotoxy(40,8);
cout<<"2.DISPLAY RECORDn";
gotoxy(40,9);
cout<<"3.SEARCH RECORDn";
gotoxy(40,10);
cout<<"4.MODIFY RECORDn";
gotoxy(40,11);
cout<<"5.DELETE RECORDn";
gotoxy(40,12);
cout<<"6.PROPERTIESn";
gotoxy(40,13);
cout<<"0.EXITn";
gotoxy(45,18);
cout<<"Enter Your Choice: ";
ch=getche();
switch(ch)
{
case '1': obj.input_record();break;
case '2': obj.putdata_record();break;
case '3': obj.search_record(); break;
case '4': obj.modify_record(); break;
case '5': obj.delete_record();break;
case '6': obj.total();break;
case '0': exit(0); //program exit
}
}
while(ch!=0);
getch();
}
OUTPUT
SELECTION MENU
1.INSERT RECORD
2.DISPLAY RECORD
3.SEARCH RECORD
4.MODIFY RECORD
5.DELETE RECORD
6.PROPERTIES
0.EXIT
Enter Your Choice:
अब आप स्वयं से एक project बना सकते हैं।
यदि आप चाहें, तो आप database backup , reset database , database recovery, user login page, multiple user access आदि जैसे अन्य विकल्पों को जोड़कर इसे और बेहतर बना सकते हैं। इससे आपकी file handling के साथ-साथ आपके programming skill में सुधार होगा। जो concept clear करने से बेहतर है।
Related Project in C++ are:
- Array of Object Examples in C++ (Student Management System)
- Bank Management System in C++
- Print a student mark-sheet in C++
- Billing Management System in C++
- Hotel Management System in C++
C++ Examples