首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

小弟我这有一个 但是 就是没法操作文件 请

2012-02-07 
我这有一个 但是 就是没法操作文件请高手指点#include string.h#include malloc.h#include iostream.

我这有一个 但是 就是没法操作文件 请高手指点
#include <string.h>
#include <malloc.h>
#include <iostream.h>
#include <conio.h>
//#define DATAFILE "data.dat" //包含信息
class room //定义客房类
{
 
  public:
  char rname[10];  
  int set;
  room(){}
  room(char *rn,int se)
  {
  strcpy(rname,rn);
  set=se;
  }
};
  room rm[5]={room("111",1),room("112",1),room("113",1),room("114",1),room("115",1)};

typedef struct node //定义一个结构体
{ char n[20];//姓名
  char wid[20];//身份证件号码
  char sex[5];
  char tel[10];
  char position[20];//房间档次
  char num[10];//房间号码
  char birth[20];
  //char depart[20];//部门
  char addr[35];
  struct node *next; //定义节点*next
}NODE;
 
  class customer //定义顾客类
  {
  private:
  NODE *p; //定义节点*p  
  NODE *q; ////定义节点*q
  NODE *t; //定义节点*t
  public:
  customer(NODE *m); //构造函数
  void add();
  void empty();
  void print();
  void infin(char *n,char *rna)
  { p=q;
 
  while (p!=t&&(strcmp(p->wid,n)!=0)!=0)
  {p=p->next;
  }
  if (p==t)
  {cout<<"查无此人!!"<<endl;
  }
  else
  { strcpy(p->num,rna);
  cout<<"订房完成!"<<endl;
  }  

  }
  char*infou(char *n)
  {char *rn;
  rn=new char[10];
  p=q;
 
  while (p!=t&&(strcmp(p->wid,n)!=0)!=0)
  {p=p->next;
  }
  if (p==t)
  {cout<<"查无此人!!"<<endl;
  }
  else
  { strcpy(rn,p->num);
  strcpy(p->num,"0");
  return rn;
  }

  }
   
  };

  customer::customer(NODE *m)
  { t=p=m;
  }
 
  void customer::print()  
  { p=q;
  char n[20];
  cout<<"请输入身份证号:"<<endl;
  cin>>n;
  while (p!=t&&(strcmp(p->wid,n)!=0)!=0)
  {p=p->next;
  }
  if (p==t)
  {cout<<"查无此人!!"<<endl;
  }
  else
  { cout<<"姓名:"<<p->n<<endl;
  cout<<"身份证号:"<<p->wid<<endl;
  cout<<"性别:"<<p->sex<<endl;
  cout<<"家庭电话:"<<p->tel<<endl;
  cout<<"入住号码:"<<p->num<<endl;
  cout<<"房间档次:"<<p->position<<endl;
  cout<<"家庭住址:"<<p->addr<<endl;
  }
  cout<<"按任意键返回!!"<<endl;
  }
  void customer::empty(){
  for(int j=0;j<5;j++)
  {
  if(rm[j].set==1)
  {
  cout<<rm[j].rname<<endl;
  }
  }
  cout<<"按任意键返回!!"<<endl;
  }
 
  void customer::add()
 {
  q= new NODE;
  q->next=p;
  p=q;
  cout<<"姓名:";
  cin>>q->n;
  cout<<"身份证号:";


  cin>>q->wid;
  cout<<"性别:";
  cin>>q->sex;
  cout<<"联系电话:";
  cin>>q->tel;
  cout<<"房间号码:";
  cin>>q->num;
  cout<<"入住档次:";
  cin>>q->position;
  cout<<"家庭住址:";
  cin>>q->addr;
 cout<<"登记成功!你可以进入你的房间了!"<<endl;
 cout<<"按任意键返回"<<endl;
}

 void display()

  cout<<"\n***********************欢迎使用我的客房管理系统*********************************"<<endl;

  cout<<"\n 1:登记入住!"<<endl;
  cout<<"\n 2.查询空房间 " <<endl; 
  cout<<"\n 3:查找顾客信息!"<<endl;
  cout<<"\n 4:顾客订房!"<<endl;
  cout<<"\n 5:顾客退房!"<<endl;
  cout<<"\n 6:退出系统!"<<endl; 
  cout<<"\n********************************************************************************"<<endl;
}

  void main()  
 {cout<<"请你选择操作:"<<endl;
int j;
  char n[20],rna[10];
  NODE *pa;
  char i;
  pa=(NODE*)malloc(sizeof(struct node));
  display();
  customer a(pa);
  while(i=getch())
  {
  switch(i)
  {
  case'1':a.add();getch();display();break;
  case'2':a.empty();getch();display();break;
  case'3':a.print();getch();display();break;
  case '4': 
  cout<<"请输入要订房的身份证号:"<<endl;
  cin>>n;
  cout<<"目前的空房间号码有:"<<endl;
  for(j=0;j<5;j++)
  {
  if(rm[j].set==1)
  {
  cout<<rm[j].rname<<endl;
   
  }
  }
  cout<<"请输入你要订的房间号码:";
  cin>>rna;
  for(j=0;j<5;j++)
  {
  if(strcmp(rm[j].rname,rna)==0)
  {
  rm[j].set=0;
   
  }
  }
  a.infin(n,rna);
  getch();display();break;
   
  case '5': 
  cout<<"请输入要订房的客户的身份证号:"<<endl;
  cin>>n;
  strcpy(rna,a.infou(n));
  for(j=0;j<5;j++)
  {
  if(strcmp(rm[j].rname,rna)==0)
  {
  rm[j].set=1;
   
  }
  }
  cout<<"退房成功!"<<endl;
  getch();display();break;

  case'6':cout<<"谢谢您使用本客房管理系统!"<<endl;goto end;}
  }
end: ;
}


现在的要求是顾客信息和客房信息都保存到文件中,程序从文件开始执行?怎么办啊?

[解决办法]
cfile
[解决办法]
FILE

fopen
fwrite
fread


[解决办法]
fstream 操作啊,很简单的,定义好结构,往里写就好了
[解决办法]


C/C++ code
#include  <string.h > #include  <malloc.h > #include  <iostream.h > #include  <conio.h > #include <fstream.h>//#define DATAFILE "data.dat"    //包含信息 class room            //定义客房类 {      public:     char rname[10];            int set;     room(){}    room(char *rn,int se)    {     strcpy(rname,rn);        set=se;    } };   room rm[5]={room("111",1),room("112",1),room("113",1),room("114",1),room("115",1)}; typedef struct node        //定义一个结构体 {  char n[20];//姓名    char wid[20];//身份证件号码    char sex[5];    char tel[10];    char position[20];//房间档次    char num[10];//房间号码    char birth[20];    //char depart[20];//部门    char addr[35];    struct node *next;         //定义节点*next }NODE;     class customer              //定义顾客类   {   private:   NODE *p;          //定义节点*p        NODE *q;          ////定义节点*q   NODE *t;          //定义节点*t   public:   customer(NODE *m);       //构造函数   void add();   void empty();   void print();   void infin(char *n,char *rna)   { p=q;     while (p!=t&&(strcmp(p->wid,n)!=0)!=0)     {p=p->next;      }    if (p==t)      {cout <<"查无此人!!" <<endl;      }   else   { strcpy(p->num,rna);     cout <<"订房完成!" <<endl;   }     }   char*infou(char *n)   {char *rn;    rn=new char[10];    p=q;     while (p!=t&&(strcmp(p->wid,n)!=0)!=0)     {p=p->next;      }    if (p==t)      {cout <<"查无此人!!" <<endl;      }   else   {   strcpy(rn,p->num);    strcpy(p->num,"0");    return rn;   }   }       };   customer::customer(NODE *m)   { t=p=m;   }     void customer::print()                                           { p=q;   char n[20];   cout <<"请输入身份证号:" <<endl;   cin >>n;   while (p!=t&&(strcmp(p->wid,n)!=0)!=0)     {p=p->next;      }    if (p==t)      {cout <<"查无此人!!" <<endl;      }   else     {            ofstream outfile("g:\\information.txt");           outfile <<"姓名:" <<p->n <<endl;   outfile <<"身份证号:" <<p->wid <<endl;   outfile <<"性别:" <<p->sex <<endl;   outfile <<"家庭电话:" <<p->tel <<endl;   outfile <<"入住号码:" <<p->num <<endl;   outfile <<"房间档次:" <<p->position <<endl;   outfile <<"家庭住址:" <<p->addr <<endl;     }   cout <<"按任意键返回!!" <<endl;    }   void customer::empty(){    for(int j=0;j <5;j++)      {     if(rm[j].set==1)     {      cout <<rm[j].rname <<endl;   }    }    cout <<"按任意键返回!!" <<endl;   }     void customer::add()  {   q= new NODE;   q->next=p;   p=q;   cout <<"姓名:";   cin >>q->n;   cout <<"身份证号:";   cin >>q->wid;   cout <<"性别:";   cin >>q->sex;   cout <<"联系电话:";   cin >>q->tel;   cout <<"房间号码:";   cin >>q->num;   cout <<"入住档次:";   cin >>q->position;   cout <<"家庭住址:";   cin >>q->addr;  cout <<"登记成功!你可以进入你的房间了!" <<endl;  cout <<"按任意键返回" <<endl; }  void display() {       cout <<"\n***********************欢迎使用我的客房管理系统*********************************" <<endl;                       cout <<"\n                        1:登记入住!" <<endl;                       cout <<"\n                        2.查询空房间 "  <<endl;                        cout <<"\n                        3:查找顾客信息!" <<endl;                       cout <<"\n                        4:顾客订房!" <<endl;                       cout <<"\n                        5:顾客退房!" <<endl;                       cout <<"\n                        6:退出系统!" <<endl;     cout <<"\n********************************************************************************" <<endl; }   int main()                       {cout <<"请你选择操作:" <<endl;   int j;   char n[20],rna[10];   NODE *pa;   int i;   pa=(NODE*)malloc(sizeof(struct node));   display();   customer a(pa);   while(cin>>i)   {               if(i==6)   {   cout <<"谢谢您使用本客房管理系统!" <<endl;     break;     }   else    switch(i)      {        case 1 :a.add();getch();display();break;        case 2 :a.empty();getch();display();break;        case 3 :a.print();getch();display();break;        case  4 :          cout <<"请输入要订房的身份证号:" <<endl;        cin >>n;        cout <<"目前的空房间号码有:" <<endl;        for(j=0;j <5;j++)      {     if(rm[j].set==1)     {      cout <<rm[j].rname <<endl;           }      }      cout <<"请输入你要订的房间号码:";      cin >>rna;      for(j=0;j <5;j++)      {     if(strcmp(rm[j].rname,rna)==0)     {      rm[j].set=0;           }      }      a.infin(n,rna);      getch();display();break;          case  5 :       cout <<"请输入要订房的客户的身份证号:" <<endl;      cin >>n;      strcpy(rna,a.infou(n));      for(j=0;j <5;j++)      {     if(strcmp(rm[j].rname,rna)==0)     {      rm[j].set=1;           }      }       cout <<"退房成功!" <<endl;       getch();display();break;    } }system("pause");return 0;                } /*不知道是不是这样子的?*? 

热点排行