C++从文件读取链表,或者将链表中的数据提取出来
#include<iostream>#include<fstream>#include<string>usingnamespacestd;#defineNULL0intn...
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
#define NULL 0
int n;
class Goods
{
public:
//Goods(){serial_number=0;name="mingzi";address="pingdingshan";unit_price=0;count=0;}
long int serial_number;
string name;
string address;
double unit_price;
int count;
Goods *next;
};
Goods *head,*p1,*p2,*p;
void read_from_file()
{
ifstream infile("file.dat",ios::binary|ios::in);
if(!infile)
{
cerr<<"open error!"<<endl;
abort();
}
p1=p2=new Goods;
n=0;
while(! infile.eof())
{
n++;
infile.read((char*)p1,sizeof(Goods));
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=new Goods;
}
infile.close();
}
void creat_list()
{
n=0;
p1=p2=new Goods;
while(1)
{
n++;
if(n==1)
{
cout<<endl<<"输入商品代码:";cin>>p1->serial_number;
cout<<endl<<"输入商品名称:";cin>>p1->name;
cout<<endl<<"输入商品生产地址:";cin>>p1->address;
cout<<endl<<"输入商品单价:";cin>>p1->unit_price;
cout<<endl<<"输入存储商品数量:";cin>>p1->count;
head=p1;
}
else
{
p1=new Goods;
p2->next=p1;
p2=p1;
cout<<endl<<"输入商品代码(不再输入,请按0):";cin>>p1->serial_number;
if(p1->serial_number==0)
{ p2->next=NULL; break;}
cout<<endl<<"输入商品名称:";cin>>p1->name;
cout<<endl<<"输入商品生产地址:";cin>>p1->address;
cout<<endl<<"输入商品单价:";cin>>p1->unit_price;
cout<<endl<<"输入存储商品数量:";cin>>p1->count;
}
}
}
void write_to_file()
{
ofstream outfile("file.dat",ios::binary);
if(!outfile)
{
cerr<<"open error"<<endl;
abort();
}
p=head;
while(p->next!=NULL)
{
outfile.write((char*)p,sizeof(Goods));
p=p->next;
}
outfile.close();
}
void input_data()
{
p=head;
while(p!=NULL)
{
cout<<p->serial_number<<endl;
cout<<p->name<<endl;
cout<<p->address<<endl;
cout<<p->unit_price<<endl;
cout<<p->count<<endl;
p=p->next;
}
}
int main()
{
void write_to_file() ;
void creat_list();
void read_from_file();
void input_data();
creat_list();
write_to_file();
read_from_file();
input_data();
return 0;
}
编译时没有错误,程序中的write_to_file()和read_from_file()两个函数应该有错误。。但我不知道错到哪了,大侠们能否帮我解决一下。。。我用的编译环境是VC++6.0...急急急!! 展开
#include<fstream>
#include<string>
using namespace std;
#define NULL 0
int n;
class Goods
{
public:
//Goods(){serial_number=0;name="mingzi";address="pingdingshan";unit_price=0;count=0;}
long int serial_number;
string name;
string address;
double unit_price;
int count;
Goods *next;
};
Goods *head,*p1,*p2,*p;
void read_from_file()
{
ifstream infile("file.dat",ios::binary|ios::in);
if(!infile)
{
cerr<<"open error!"<<endl;
abort();
}
p1=p2=new Goods;
n=0;
while(! infile.eof())
{
n++;
infile.read((char*)p1,sizeof(Goods));
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=new Goods;
}
infile.close();
}
void creat_list()
{
n=0;
p1=p2=new Goods;
while(1)
{
n++;
if(n==1)
{
cout<<endl<<"输入商品代码:";cin>>p1->serial_number;
cout<<endl<<"输入商品名称:";cin>>p1->name;
cout<<endl<<"输入商品生产地址:";cin>>p1->address;
cout<<endl<<"输入商品单价:";cin>>p1->unit_price;
cout<<endl<<"输入存储商品数量:";cin>>p1->count;
head=p1;
}
else
{
p1=new Goods;
p2->next=p1;
p2=p1;
cout<<endl<<"输入商品代码(不再输入,请按0):";cin>>p1->serial_number;
if(p1->serial_number==0)
{ p2->next=NULL; break;}
cout<<endl<<"输入商品名称:";cin>>p1->name;
cout<<endl<<"输入商品生产地址:";cin>>p1->address;
cout<<endl<<"输入商品单价:";cin>>p1->unit_price;
cout<<endl<<"输入存储商品数量:";cin>>p1->count;
}
}
}
void write_to_file()
{
ofstream outfile("file.dat",ios::binary);
if(!outfile)
{
cerr<<"open error"<<endl;
abort();
}
p=head;
while(p->next!=NULL)
{
outfile.write((char*)p,sizeof(Goods));
p=p->next;
}
outfile.close();
}
void input_data()
{
p=head;
while(p!=NULL)
{
cout<<p->serial_number<<endl;
cout<<p->name<<endl;
cout<<p->address<<endl;
cout<<p->unit_price<<endl;
cout<<p->count<<endl;
p=p->next;
}
}
int main()
{
void write_to_file() ;
void creat_list();
void read_from_file();
void input_data();
creat_list();
write_to_file();
read_from_file();
input_data();
return 0;
}
编译时没有错误,程序中的write_to_file()和read_from_file()两个函数应该有错误。。但我不知道错到哪了,大侠们能否帮我解决一下。。。我用的编译环境是VC++6.0...急急急!! 展开
2个回答
展开全部
outfile.write((char*)p,sizeof(Goods)); 确实有问题
根本问题在于string 的定义问题,如果它里面只是动态申请空间,你这样write只是把空间地址写到文件中,并没有把数据内容写到文件中。
同样读也有这样的问题。
你可以在这个类里面定义write,read函数,把内成员一个个写到文件中或者读进来。
还有next指针写道文件中意义不大,应该考虑另外一种方式,比如序号,或者根本不写到文件中。
根本问题在于string 的定义问题,如果它里面只是动态申请空间,你这样write只是把空间地址写到文件中,并没有把数据内容写到文件中。
同样读也有这样的问题。
你可以在这个类里面定义write,read函数,把内成员一个个写到文件中或者读进来。
还有next指针写道文件中意义不大,应该考虑另外一种方式,比如序号,或者根本不写到文件中。
更多追问追答
追问
我刚学完C++,感觉你说的比较深奥。。。能不能帮我修改一下程序。。谢谢了。。
追答
你要是不愿意写这个函数,就这样:
long int serial_number;
string name;
string address;
double unit_price;
int count;
outfile.write((char*)p,sizeof(Goods));
==〉
outfile.write(&(p->serial_number),sizeof(long));
int len = strlen(p->name);
outfile.write(&(len),sizeof(int));
outfile.write((const char*)p->name, len);
...............
infile.read((char*)p1,sizeof(Goods));
==>
infile.read((char*)(&p1->serial_number),sizeof(long));
int len ;
infile.read((char*)(&len),sizeof(int));
char tbuf[10000];
infile.read((char*)(tbuf),len);
p1->name = tbuf;
..........
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
//希望对楼主有所帮助。。。
//楼主,按照你意图,程序已经调通了,基本符合功能需要了
//不过,程序中,没有对于动态分配的空间进行释放,楼主可以自己加上去。
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
#define NULL 0
int n;
class Goods
{
public:
//Goods(){serial_number=0;name="mingzi";address="pingdingshan";unit_price=0;count=0;}
long int serial_number;
string name;
string address;
double unit_price;
int count;
Goods *next;
};
Goods *head,*p1,*p2,*p;
void read_from_file()
{
ifstream infile("file.dat",ifstream::binary);
if(!infile)
{
cerr<<"open error!"<<endl;
abort();
}
n=0;
while(! infile.eof())
{
p1=p2=new Goods;
n++;
infile.read((char*)p1,sizeof(Goods));
if(n==1)
{
head=p1;
if(p1->next==NULL)break;
}
else
{
p2->next=p1;
p2=p1;
if(p1->next==NULL)break;
}
}
infile.close();
}
void creat_list()
{
n=0;
p1=p2=new Goods;
int temp;
string strTemp;
while(1)
{
n++;
if(n==1)
{
cout<<endl<<"输入商品代码:";cin>>p1->serial_number;
cout<<endl<<"输入商品名称:";cin>>p1->name;
cout<<endl<<"输入商品生产地址:";cin>>p1->address;
cout<<endl<<"输入商品单价:";cin>>p1->unit_price;
cout<<endl<<"输入存储商品数量:";cin>>p1->count;
head=p1;
p1->next=NULL;
}
else
{
cout<<endl<<"输入商品代码(不再输入,请按0):";cin>>temp;
if(temp==0)break;
p1=new Goods;
p2->next=p1;
p2=p1;
p1->serial_number=temp;
cout<<endl<<"输入商品名称:";cin>>p1->name;
cout<<endl<<"输入商品生产地址:";cin>>p1->address;
cout<<endl<<"输入商品单价:";cin>>p1->unit_price;
cout<<endl<<"输入存储商品数量:";cin>>p1->count;
p1->next=NULL;
}
}
}
void write_to_file()
{
ofstream outfile;
outfile.open("file.dat",ofstream::binary);
if(!outfile)
{
cerr<<"open error"<<endl;
abort();
}
p=head;
while(p!=NULL)
{
outfile.write((char*)p,sizeof(Goods));
p=p->next;
}
outfile.close();
}
void input_data()
{
p=head;
while(p!=NULL)
{
cout<<"serial_number:"<<p->serial_number<<endl;
cout<<"name:"<<p->name<<endl;
cout<<"address:"<<p->address<<endl;
cout<<"unit_price:"<<p->unit_price<<endl;
cout<<"count:"<<p->count<<endl;
p=p->next;
}
}
int main()
{
void write_to_file() ;
void creat_list();
void read_from_file();
void input_data();
creat_list();
write_to_file();
read_from_file();
input_data();
return 0;
}
//楼主,按照你意图,程序已经调通了,基本符合功能需要了
//不过,程序中,没有对于动态分配的空间进行释放,楼主可以自己加上去。
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
#define NULL 0
int n;
class Goods
{
public:
//Goods(){serial_number=0;name="mingzi";address="pingdingshan";unit_price=0;count=0;}
long int serial_number;
string name;
string address;
double unit_price;
int count;
Goods *next;
};
Goods *head,*p1,*p2,*p;
void read_from_file()
{
ifstream infile("file.dat",ifstream::binary);
if(!infile)
{
cerr<<"open error!"<<endl;
abort();
}
n=0;
while(! infile.eof())
{
p1=p2=new Goods;
n++;
infile.read((char*)p1,sizeof(Goods));
if(n==1)
{
head=p1;
if(p1->next==NULL)break;
}
else
{
p2->next=p1;
p2=p1;
if(p1->next==NULL)break;
}
}
infile.close();
}
void creat_list()
{
n=0;
p1=p2=new Goods;
int temp;
string strTemp;
while(1)
{
n++;
if(n==1)
{
cout<<endl<<"输入商品代码:";cin>>p1->serial_number;
cout<<endl<<"输入商品名称:";cin>>p1->name;
cout<<endl<<"输入商品生产地址:";cin>>p1->address;
cout<<endl<<"输入商品单价:";cin>>p1->unit_price;
cout<<endl<<"输入存储商品数量:";cin>>p1->count;
head=p1;
p1->next=NULL;
}
else
{
cout<<endl<<"输入商品代码(不再输入,请按0):";cin>>temp;
if(temp==0)break;
p1=new Goods;
p2->next=p1;
p2=p1;
p1->serial_number=temp;
cout<<endl<<"输入商品名称:";cin>>p1->name;
cout<<endl<<"输入商品生产地址:";cin>>p1->address;
cout<<endl<<"输入商品单价:";cin>>p1->unit_price;
cout<<endl<<"输入存储商品数量:";cin>>p1->count;
p1->next=NULL;
}
}
}
void write_to_file()
{
ofstream outfile;
outfile.open("file.dat",ofstream::binary);
if(!outfile)
{
cerr<<"open error"<<endl;
abort();
}
p=head;
while(p!=NULL)
{
outfile.write((char*)p,sizeof(Goods));
p=p->next;
}
outfile.close();
}
void input_data()
{
p=head;
while(p!=NULL)
{
cout<<"serial_number:"<<p->serial_number<<endl;
cout<<"name:"<<p->name<<endl;
cout<<"address:"<<p->address<<endl;
cout<<"unit_price:"<<p->unit_price<<endl;
cout<<"count:"<<p->count<<endl;
p=p->next;
}
}
int main()
{
void write_to_file() ;
void creat_list();
void read_from_file();
void input_data();
creat_list();
write_to_file();
read_from_file();
input_data();
return 0;
}
追问
敢问你的QQ。。有问题还想再问你。。。真谢谢了。。。我的QQ就是我的百度名。。。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询