学生输入数据未永久存储在C ++中的dat文件中

时间:2018-12-20 04:49:25

标签: c++ visual-c++

我使用c ++将学生信息存储在一个.dat / .txt文件中。它工作正常,但是每当我停止程序并再次启动时,以前的数据都将被删除,我需要捕获以前的数据,新数据也有助于解决此问题,我在此附上了我尝试过的代码。 >

# include<iostream.h>
# include<conio.h>
# include<stdio.h>
# include<string.h>
# include<stdlib.h>
# include<fstream.h>
class admission
{ char name[20];
char gen[20];
int age;
int code;
char house[30];
public:
voidgetinfo()
{ cout<<"\nenter the name of the person";
gets(name);
cout<<"\n witch or a wizard";
cin>>gen;
cout<<"\n enter the age of the student";
cin>>age;
cout<<"\n enter the student register number";
cin>>code;
sorting();
}
void sorting();
voidputinfo()
{ cout<<"\n********************student details************************";
cout<<"\n name:"<<name;
cout<<"\n age:"<<age;
cout<<"\n gen:"<<gen;
cout<<"\n code:"<<code;
cout<<"\n hobbies:"<<hbby;
cout<<"\n house :"<<house;
}
intccode()
{return code;}
void modify()
{  cout<<"\n name:"<<name;
cout<<"\n age:"<<age;
cout<<"\n gen:"<<gen;
cout<<"\n code:"<<code;
cout<<"\n enter new details";
char nname[40],ngen[20],nage;
intnage;
cout<<"\n new name:";
cin>>nname;
cout<<"\n new age:";
cin>>nage;
cout<<"\n gen:";
cin>>ngen;
}
}p,stud;
void admission::sorting()
{ randomize();
int p=random(4);
switch(p)
{ case 0:strcpy(house,"gryffindor");
    break;
case 1:strcpy(house,"ravenclaw");
    break;
case 2:strcpy(house,"hufflepuff");
    break;
case 3:strcpy(house,"slytherin");
    break;
}
}
class fees
{ intclss;
float cgpa;
public:
floatcgp()
{cout<<"\n enter cgpa";
cin>>cgpa;
returncgpa;}
intcss()
{ cout<<"\n enter class";
cin>>clss;
returnclss;}
}q;

class student
{
    int m_potions,m_dada,m_transfiguration,m_charms;
public:
voidgetmarks()
{ cout<<"\n enter the marks for potions:";
cin>>m_potions;
cout<<"\n enter the marks for defence against dark arts:";
cin>>m_dada;
cout<<"\n enter the marks for transfiguration:";
cin>>m_transfiguration;
cout<<"\n enter the marks for charms";
cin>>m_charms;
 }
voidputmarks()
{ cout<<"\n potions:"<<m_potions;
cout<<"\n defence against dark arts:"<<m_dada;
cout<<"\n transfiguration:"<<m_transfiguration;
 }
floatavgg()
{ return (m_dada+m_transfiguration+m_potions)/3;}

}w;

void main()
{
clrscr();
fstream f;
intcal();
inttotalcal();
intgetpwd();
charans;
intch;
char password[50];
char user[50];

do
{ cout<<"\n*********school*************";
cout<<"\n 1.admission:";
cout<<"\n 2.student details by register no:";
cout<<"\n 3.fee structure:";
cout<<"\n 4.modification of student details:";
cout<<"\n 5.marks of students:";
cout<<"\n 6.deletion";
cout<<"\n enter your choice";
cin>>ch;

switch(ch)
{ 

case 1:cout<<"\n***********admission***********";
    intn,i=0;
    int s=getpwd();
    if(s==1)
          { f.open("student.dat",ios::binary|ios::in|ios::out|ios::trunc);
    cout<<"\n enter the no of records";
    cin>>n;
    while(i<n)
        { cout<<"\n enter the details of student"<<i+1;
        p.getinfo();
        f.write((char*)&p,sizeof(p));
        i++;
        }}
        f.close();
break;

case 2:cout<<"\n*************student details*******";
    int l;
    int  g=getpwd();
    if(g==1)
        { cout<<"\n enter the register no you want to search";
        cin>>l;
        f.open("student.dat",ios::binary|ios::in|ios::out);
        while(f.read((char*)&p,sizeof(p)))
        if(p.ccode()==l)
        p.putinfo();}
        f.close();
break;

case 3:cout<<"\n *************fee structure*********";
    int k=cal();
    int t=totalcal();
    cout<<"\n total fees to be paid is:"<<k+t;
break;

case 4:cout<<"\n*************modification******************";
    f.open("student.dat",ios::binary|ios::in|ios::out);
    intcno;
    long pos;
    char found='f';
    cout<<"\n enter register no whose record is to be modified";
    cin>>cno;
    while(!f.eof())
    { pos=f.tellg();
        f.read((char *)&p,sizeof(p));
        if(p.ccode()==cno)
        { p.modify();
        f.seekg(pos);
        f.write((char *)&p,sizeof(p));
        found='t';
        break;
           }
           }
    if(found=='f')
    cout<<"\n record not found";
    f.seekg(0);
    cout<<"\n now the file contains";
    while(!f.eof())
          {
    f.read((char*)&p,sizeof(p));
    p.putinfo();
          }
    f.close();
break;


    case 5:cout<<"\n*****************marks*******************";
    f.open("student.dat",ios::binary|ios::in|ios::out);
    cout<<"\n enter the codeof the student";
    cin>>n;
        while(!f.eof())
        {f.read((char*)&w,sizeof(w))
         if(p.ccode()==n) 
    { cout<<"\n enter marks for student"<<i+1;
        w.getmarks();
        f.write((char*)&w,sizeof(w));
        cout<<"\n the average is"<<w.avgg();
    }}
    f.close();
    break;
    case 6:cout<<"\n*************deletion***************";
    intsno;
    charffound='f',confirm='n';
    ifstream f1("student.dat",ios::in);
    ofstream file("temp.dat",ios::out);
    cout<<"\n enter the register no of the student to be deleted";
    cin>>sno;
    while(!f1.eof())
        { f1.read((char*)&p,sizeof(p));
        if(p.ccode()==sno)
        { p.putinfo();
        ffound='t';
        cout<<"\n are you sure you wanna delete this";
        cin>>confirm;
        if(confirm=='n')
        file.write((char*)&p,sizeof(p));
           }
        else
        continue;
          }
    if(ffound=='f')
        cout<<"\n record not found";
    f1.close();
    file.close();
    remove("student.dat");
    rename("temp.dat","student.dat");
    break;
defaut:cout<<"\n enter valid option";
}
cout<<"\n do you wanna continue";
cin>>ans;
}while(ans=='y'||ans=='Y');
getch();
}
intcal()
{ intfe;
if(q.css()==11)
{ if(q.cgp()==10)
fe=10000;
else if((q.cgp()>9)&&(q.cgp()<10))
fe=11000;
else
fe=12000;
      }
else
{ if(q.cgp()==10)
    fe=12000;
    else if((q.cgp()>9)&&(q.cgp()<10))
    fe=12500;
    else
    fe=13000;
       }
returnfe;
}
inttotalcal()
{ char chh,ch1;
int fee1;
cout<<"\n do you like to avail transport";
cin>>chh;
if(chh=='y')
{  cout<<"\n would you like to travel in a bus or  van :for bus press b,for van press f";
cin>>ch1;
if(ch1=='f')
       fee1=5000;
else
      fee1=6000;
return fee1;}
else
return 0;
 }
intgetpwd()
{ charusr[50];
charpwd[20];
char password[20];
char user[50];
cout<<"\n enter the username";
cin>>usr;
cout<<"\n enter the password";
cin>>pwd;
strcpy(password,"albus");
strcpy(user,"School");
    if((strcmp(password,pwd)==0)&&(strcmp(user,usr)==0))
return 1;
else
{ cout<<"\n enter a valid choice";
cout<<"\n you are not an authorised user";
return 0; }
}

0 个答案:

没有答案
相关问题