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

求教一个有关c++文件输入输出的有关问题

2012-03-30 
求教一个有关c++文件输入输出的问题最近我编写了一个同学录的程序在测试其中文件的存取函数时发现了一个问

求教一个有关c++文件输入输出的问题
最近我编写了一个同学录的程序  
在测试其中文件的存取函数时发现了一个问题,就是虽然程序在编译时不出错误,也能运行,但是运行的结果却不正确,从文件读出的数据都是一些乱码,在这里我附上程序的代码,希望各位高人给予解答,十分感谢!!!!!  
#include   <iostream>  
#include   <string>  
#include   <iomanip>  
#include   <fstream>  
using   namespace   std;  

class   Data   //定义数据基类  
{  
public:  
Data(){};  
virtual   int   Compare(Data   *)=0;  
virtual   void   Show()=0;  
virtual   ~Data(){};  
};  

class   Node   //定义节点基类  
{  
private:  
Data   *   sdata;  
Node   *   snext;  
public:  
Node(){sdata=NULL;snext=NULL;}  
Node(Node   &node)  
{  
sdata=node.sdata;  
snext=node.snext;  
}  
void   InputData(Data   *sdata1)   {sdata=sdata1;}  
void   ShowNode()   {sdata-> Show();}  
Data   *GetData()   {return   sdata;}  
friend   class   List;  
friend   void   StoreFile(List   &);  
};  

class   List   //定义链表基类  
{  
Node   *head;  
public:  
List();  
~List(){DeleteList();}  
void   AddNode(Node   *snode);  
Node   *DeleteNode(Node   *);  
Node   *LookUp(Data   &);  
void   ShowList();  
void   DeleteList();  
Node   *GetListHead(){return   head;}  
Node   *GetNextNode(Node   *snode)   {return   snode-> snext;}  
};  

List::List()  
{  
Node   *p;  
p=new   Node;  
p-> snext=p;  
head=p;  
}  

void   List::AddNode(Node   *snode)  
{  
Node   *q,*temp;  
Data   *p=snode-> sdata;  
q=head-> snext;  
temp=head;  
while((q!=head)&&(p-> Compare(q-> sdata))==(1))  
{  
temp=q;  
q=q-> snext;  
}  
snode-> snext=q;  
temp-> snext=snode;  
}  

Node   *List::DeleteNode(Node   *   snode)  
{  
Node   *p1,*p2;  
p1=head-> snext;  
p2=head;  
while(p1!=snode)  
{  
p2=p1;  
p1=p1-> snext;  
}  
p2-> snext=p1-> snext;  
return   snode;  
}  

Node   *   List::LookUp(Data   &   data)  
{  
Node   *p=head-> snext;  
Data   *q=&   data;  
while(p!=head)  
{  
if(p-> sdata-> Compare(q)==0)  
return   p;  
p=p-> snext;  
}  
return   0;  
}  

void   List::ShowList()  
{  
Node   *p=head;  
while(p-> snext!=head)  
{  
p=p-> snext;  
(p-> sdata)-> Show();  
}  
}  

void   List::DeleteList()  
{  
Node   *p1,*p2;  
p1=head-> snext;  
while(p1!=head)  
{  
delete   p1-> sdata;  
p2=p1;  


p1=p1-> snext;  
delete   p2;  
}  
}  

class   StudentRecord:public   Data  
{  
private:  
string   Name;  
string   Phone;  
string   Address;  
public:  
StudentRecord()   {Name= "\0 ";Phone= "\0 ";Address= "\0 ";}  
StudentRecord(string   n,string   p,string   a):Name(n),Phone(p),Address(a){}  
void   SetRecord(string   n,string   p,string   a)  
{  
Name=n;   Phone=p;   Address=a;  
}  
int   Compare(Data   *);  
void   Show();  
};  

int   StudentRecord::Compare(Data   *   data)  
{  
StudentRecord   *temp=(StudentRecord   *)data;  
if(Name <temp-> Name)   return   -1;  
if(Name==temp-> Name)   return   0;  
if(Name> temp-> Name)   return   1;  
}  

void   StudentRecord::Show()  
{  
cout < <setw(15) < <Name < <setw(15) < <Phone < <setw(15) < <Address < <endl;  
}  

void   AddRecord(List   &);  
void   Display(List   &);  
void   LookUp(List   &);  
void   Delete(List   &);  
void   StoreFile(List   &);  
void   Operate(int,List   &);        
void   LoadFile(List   &);  

int   main()  
{  
List   Student;  
LoadFile(Student);  
cout < < "       欢迎进入同学录数据系统         " < <endl;  
int   choice;  
do  
{  
cout < < "1.   添加同学录记录.     " < <endl;  
cout < < "2.   显示全部记录.       " < <endl;  
cout < < "3.   根据姓名删除数据.       " < <endl;  
cout < < "4.   根据姓名查询数据.       " < <endl;  
cout < < "0.   退出系统.               " < <endl;  
cout < < "请输入您的选择: ";  
cin> > choice;  
Operate(choice,Student);  
}while(choice!=0);  
return   0;  
}  

void   Operate(int   x,List   &   Student)  
{  
switch(x)  
{  
case   1:   AddRecord(Student);break;  
case   2:   Display(Student);break;  
case   3:   Delete(Student);break;  
case   4:   LookUp(Student);break;  
case   0:   StoreFile(Student);break;  
}  
}  

void   AddRecord(List   &   Student)  
{  
Node   *   snode;  
StudentRecord   *   stu;  
string   Name,Phone,Address;  
cout < < "请输入姓名:     ";  
cin> > Name;  
while(Name!= "0 ")  
{  
cout < < "请输入电话和地址: " < <endl;  
cin> > Phone> > Address;  
stu=new   StudentRecord;  
stu-> SetRecord(Name,Phone,Address);  
snode=new   Node;  
snode-> InputData(stu);  


Student.AddNode(snode);  
cout < < "请输入姓名:     ";  
cin> > Name;  
}  
cout < <endl;  
}  

void   Display(List   &   Student)  
{  
cout < <setw(15) < < "姓名 " < <setw(15) < < "电话号码 " < <setw(15) < < "地址 " < <endl;  
Student.ShowList();  
cout < <endl;  
}  

void   Delete(List   &   Student)  
{  
Node   *   look;  
string   name;  
cout < < "请输入您想要删除的姓名(0结束) ";  
cin> > name;  
while(name!= "0 ")  
{  
StudentRecord   stu(name, "0 ", "0 ");  
look=Student.LookUp(stu);  
if(look)  
{  
cout < < "在同学录中找到: " < <name < < ",内容是: " < <endl;  
look-> ShowNode();  
Student.DeleteNode(look);  
cout < <name < < "的资料已删除 " < <endl;  
delete   look;  
}  
else  
cout < < "在同学录中找不到 " < <name < < "! " < <endl;  
cout < < "请输入您想要删除的姓名(0结束) ";  
cin> > name;  
}  
cout < <endl;  
}  

void   LookUp(List   &   Student)  
{  
Node   *   look;  
string   Name;  
cout < < "请输入您需要查找的姓名(0结束) ";  
cin> > Name;  
while(Name!= "0 ")  
{  
StudentRecord   stu(Name, "0 ", "0 ");  
look=Student.LookUp(stu);  
if(look)  
{  
cout < < "在同学录中找到: " < <Name < < ",内容是: " < <endl;  
look-> ShowNode();  
}  
else  
cout < < "在同学录中找不到 " < <Name < < "! " < <endl;  
cout < < "请输入您要查找的姓名(0结束): ";  
cin> > Name;  
}  
cout < <endl;  
}  

void   StoreFile(List   &   Student)  
{  
string   file= "STUDENT.DAT ";   //设定存储的文件名  
ofstream   outfile(file.c_str(),ios::binary);   //将文件名转成c的格式  
if(!outfile)  
{  
cout < < "数据文件打开错误! " < <endl;  
return;  
}  
Node   *   node;  
StudentRecord   *   stu;  
node=Student.GetListHead()-> snext;  
while(node!=Student.GetListHead())  
{  
stu=(StudentRecord   *)node-> GetData();   //stu为指向学生数据派生类的指针  
outfile.write((char   *)stu,sizeof(StudentRecord));   //将数据存入文件  
node=Student.GetNextNode(node);   //指向下一节点  
}  
outfile.close();  
}  

void   LoadFile(List   &   Student)  
{  
string   file= "STUDENT.DAT ";  
ifstream   infile(file.c_str(),ios::binary);  
if(!infile)  
{  
cout < < "没有数据文件! ";  
return;  
}  
Node   *   node;  
StudentRecord   *   stu;  
while(!infile.eof())  
{  
stu=new   StudentRecord;  
infile.read((char   *)stu,sizeof(StudentRecord));   //从文件将数据读入学生数据类  


node=new   Node;  
node-> InputData(stu);   //将学生数据加入节点  
Student.AddNode(node);   //将节点加入链表  
}  
infile.close();  
}  

就是最后的两个函数有问题,但我实在找不出来了,谢谢各位大哥了,帮帮忙吧~~~~  



[解决办法]
我看了下你的程序,不知道你这个程序那里有读写文件的入口了
stu=new StudentRecord;
infile.read(&stu,sizeof(StudentRecord)); //从文件将数据读入学生数据类,你把stu char了,大概是这里的原因吧,把地址给它试试
//(StudentRecord)不行加它
node=new Node;
node-> InputData(stu); //将学生数据加入节点
Student.AddNode(node); //将节点加入链表
[解决办法]
我也晕了,不过涉及文件操作的操作,如果出现乱码,而又感觉自己的程序没有啥问题,可看一下是否考虑到了字符串的结束符(如果没有,一般会乱码),另外删除或写数据时,看一下前后操作的行是否字节数一样,不然也可能会造成乱码,呵呵,我只是瞎说的

热点排行