| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 519 人关注过本帖
标题:[求助]求教一个有关c++文件输入输出的问题
只看楼主 加入收藏
mk3216086
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2007-4-16
收藏
 问题点数:0 回复次数:0 
[求助]求教一个有关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();
}

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


搜索更多相关主题的帖子: 文件 include Data 输出 
2007-04-16 18:34
快速回复:[求助]求教一个有关c++文件输入输出的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016561 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved