/Creating a file to store Students Data/
/Please use Dev C++ IDE while compiling this program/
#include iostream
#include fstream
#include conio.h
using namespace std;
class student
{
private:
char name[60], add[40];
int cl,rn,id;
public:
void getdata()
{
cout<<"Enter Your ID: ";
cin>>id;
cout<<"Name: ";
cin>>name;
cout<<"Address: ";
cin>>add;
cout<<"Class: ";
cin>>cl;
cout<<"Roll No: ";
cin>>rn;
}
void display()
{
cout<<"ID: "<<id<<endl;
cout<<"Name: "<<name<<endl;
cout<<"Address: "<<add<<endl;
cout<<"Class: "<<cl<<endl;
cout<<"Roll No. : "<<rn<<endl;
}
int getid()
{
return id;
}
}s1;
void dsn()
{
cout<<"\n\t\t\t* ** **************************** ** \n";
cout<<"\t\t\t ** ****** WELCOME ****** ** \n";
cout<<"\t\t\t ** **************************** ** \n\n\n\n";
}
void modify()
{
student st;
fstream fio("Stu.dat",ios::in|ios::out|ios::binary);
if(!fio)
{
cout<<"File could not be open !! Press any Key...";
getch();
return;
}
int d;
long pos;
char flag2=0;
cout<<"\n\nEnter The ID To Be Modified: ";
cin>>d;
while(!fio.eof())
{
pos=fio.tellg();
fio.read((char)&st,sizeof(st));
if(st.getid()==d)
{
cout<<"\n\n\t\tEnter New Details: \n\n";
st.getdata();
}
fio.seekg(pos);
fio.write((char)&st,sizeof(st));
flag2=1;
break;
}
if(flag2==0)
{
cout<<"\nRecord Not Found !!!";
}
cout<<"\n\nRecord Modified\n\n";
fio.close();
cout<<"\n\nPress a Key: ";
getch();
}
void display_all()
{
student st;
ifstream inFile;
inFile.open("Stu.dat",ios::binary);
if(!inFile)
{
cout<<"File could not be open !! Press any Key...";
getch();
return;
}
system("cls");
cout<<"\n\t\t\t ** **************************** ** \n";
cout<<"\t\t\t ** * DISPLAY ALL RECORDS * ** \n";
cout<<"\t\t\t ** **************************** ** \n\n\n\n";
while(inFile.read((char ) &st, sizeof(student)))
{
st.display();
cout<<"\n\n====================================\n";
}
inFile.close();
cout<<"\n\nPress a Key: ";
getch();
}
int del_file()
{
ifstream f;
f.open("Stu.dat",ios::binary);
if(!f)
{
cout<<"File Not Found !!! Press any Key...";
getch();
f.close();
}
else
{
f.close();
remove("Stu.dat");
return 1;
}
}
void del_recd()
{
int rec;
student st;
ifstream f1("Stu.dat",ios::binary);
ofstream f2("temp.dat",ios::out | ios::binary );
if(!f1)
{
cout<<"File could not be open !! Press any Key...";
getch();
return;
}
cout<<"\n\nEnter The ID To Delete The Record: ";
cin>>rec;
cout<<endl;
while(f1.read((char)&st,sizeof(st)))
{
if(st.getid()!=rec)
{
st.display();
cout<<"\n\n====================================\n";
f2.write((char)&st,sizeof(st));
}
}
f1.close();
f2.close();
remove("Stu.dat");
rename("temp.dat","Stu.dat");
cout<<"\n\n\t\t\tRECORD DELETED !!!\n\n";
}
int main()
{
int ch,idno,found=0,src,op;
char ag;
fstream f1;
while(1)
{ // Begin while
system("cls");
dsn(); //Call To Function dsn();
cout<<"===========================\n";
cout<<"1. ENTER RECORD |\n";
cout<<"2. DISPLAY ALL RECORDS |\n";
cout<<"3. SEARCH RECORD |\n";
cout<<"4. MODIFY RECORD |\n";
cout<<"5. DELETE RECORD |\n";
cout<<"6. DELETE FILE |\n";
cout<<"7. EXIT |\n";
cout<<"===========================\n\n";
cout<<"\nEnter your choice: ";
cin>>ch;
switch(ch)
{ // Start switch
case 1:
{
system("cls");
cout<<"\n\t\t\t* ** **************************** ** \n";
cout<<"\t\t\t ** **** ENTER RECORDS **** ** \n";
cout<<"\t\t\t ** **************************** ** \n\n\n\n";
s1.getdata();
f1.open("Stu.dat",ios::app);
f1.write((char)&s1,sizeof(student));
f1.close();
cout<<"\n\n\t\t\tRecord Added With ID: "<<s1.getid();
cout<<"\n\nPress a Key: ";
getch();
break;
}
case 2:
{
display_all(); //Call To Function display_all();
break;
}
case 3:
{
student st;
ifstream inFile;
int n;
system("cls");
cout<<"\n\t\t\t* ** **************************** ** \n";
cout<<"\t\t\t ** **** SEARCH RECORDS **** ** \n";
cout<<"\t\t\t ** **************************** ** *\n\n\n\n";
cout<<"\n\n\tPlease Enter The ID number ";
cin>>n;
inFile.open("Stu.dat",ios::binary);
if(!inFile)
{
cout<<"File could not be open !! Press any Key...";
getch();
return (1);
}
int flag=0;
while(inFile.read((char ) &st, sizeof(student)))
{
if(st.getid()==n)
{
st.display();
flag=1;
}
}
inFile.close();
if(flag==0)
{
cout<<"\n\n Record doesnot exist";
}
cout<<"\n\nPress a Key: ";
getch();
break;
}
case 4:
{
system("cls");
cout<<"\n\t\t\t ** **************************** ** \n";
cout<<"\t\t\t ** * RECORD MODIFICATION * ** \n";
cout<<"\t\t\t ** **************************** ** \n\n\n\n";
modify(); //Call To Function modify();
break;
}
case 5:
{
system("cls");
cout<<"\n\t\t\t ** **************************** ** \n";
cout<<"\t\t\t ** *** DELETE RECORD *** ** \n";
cout<<"\t\t\t ** **************************** ** *\n\n\n\n";
del_recd(); //Call To Function del_recd();
cout<<"\n\nPress a Key: ";
getch();
break;
}
case 6:
{
int p;
p=del_file(); //Call To Function del_file();
if(p==1)
{
cout<<"\n\n\t\tThe File Stu.dat is Deleted";
cout<<"\n\n\tPress a Key: ";
getch();
}
break;
}
case 7:
{
system("cls");
cout<<"\n\n\n\t\t\tThank You !!!";
exit(1);
}
default:
system("cls");
cout<<"Enter a valid choice!";
cout<<"\n\nPress a Key To Go Back To The Main Menu: ";
getch();
} // End of switch
} // End of while
return 0;
}
Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
If you enjoyed what you read here, create your account today and start earning FREE STEEM!