关于链表的C++文件读写
出处http://blog.csdn.net/u013815546/article/details/42110269
课程设计时需要构建一个商品表,我使用的是一个链表来存储一个个商品,那么在过程中需要将信息存储进文件,再从文件读取到内存的链表中。
思路不是很难,但是还是卡在了一些细节上。
细节1:存储1~n个商品时,在最后一个商品及n商品时,在文件末尾会多一个换行符,这个换行符会影响到文件的读写操作。
细节2:链表的每个商品信息在读取时都需要动态分配内存,以及一些指针的操作问题。(平时只注重于算法的学习了,对于链表不熟悉吃的亏,在此感谢队友jameslee的Debug)
细节3:对于类的存储,我想到的是用二进制而不是一般存储,因为二进制是按照字节来存储的,方便文件读写,当然文件存储信息是一个过渡,学完数据库就方便了。
文件写入商品信息:
- #include <iostream.h>
- #include <fstream.h>
- #include <stdlib.h>
- #include <cstring>
- class Goods
- {
- protected:
- char goods_name[50]; //商品名称
- int goods_number; //商品代码
- char person_name[30]; //经办人
- int price; //进货价
- int amount; //库存量
- Goods *next;//定义指向类Goods的指针变量next
- public:
- Goods(int goods_number=0,char*goods_name="null",char*person_name="null",int price=0,int amount=0)//定义构造函数
- {
- this->goods_number=goods_number;
- strcpy(this->goods_name,goods_name);
- strcpy(this->person_name,person_name);
- this->price=price;
- this->amount=amount;
- }
- void ShowData()//定义输出函数
- {
- cout<<"goods_number:"<<goods_number<<" goods_name:"<<goods_name<<" person_name:"<<person_name<<" price:"<<price<<" amount:"<<amount<<endl;
- }
- };
- int main ()
- {
- ofstream out("data.txt",ios::app|ios::binary);
- Goods x(1,"name1","das",1,1);
- Goods y(2,"name2","das2",2,2);
- Goods z(3,"name3","das3",3,3);
- if (out.is_open())
- {
- out.write((char*)&x,sizeof(x));
- out.write((char*)&y,sizeof(y));
- out.write((char*)&z,sizeof(z));
- }
- return 0;
- }
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <cstring>
class Goods
{
protected:
char goods_name[50]; //商品名称
int goods_number; //商品代码
char person_name[30]; //经办人
int price; //进货价
int amount; //库存量
Goods *next;//定义指向类Goods的指针变量next
public:
Goods(int goods_number=0,char*goods_name="null",char*person_name="null",int price=0,int amount=0)//定义构造函数
{
this->goods_number=goods_number;
strcpy(this->goods_name,goods_name);
strcpy(this->person_name,person_name);
this->price=price;
this->amount=amount;
}
void ShowData()//定义输出函数
{
cout<<"goods_number:"<<goods_number<<" goods_name:"<<goods_name<<" person_name:"<<person_name<<" price:"<<price<<" amount:"<<amount<<endl;
}
};
int main ()
{
ofstream out("data.txt",ios::app|ios::binary);
Goods x(1,"name1","das",1,1);
Goods y(2,"name2","das2",2,2);
Goods z(3,"name3","das3",3,3);
if (out.is_open())
{
out.write((char*)&x,sizeof(x));
out.write((char*)&y,sizeof(y));
out.write((char*)&z,sizeof(z));
}
return 0;
}
文件读入内存的链表中:
- #include <iostream.h>
- #include <fstream.h>
- #include <stdlib.h>
- #include <cstring>
- class Goods
- {
- public:
- char goods_name[50]; //商品名称
- int goods_number; //商品代码
- char person_name[30]; //经办人
- int price; //进货价
- int amount; //库存量
- Goods *next;//定义指向类Goods的指针变量next
- Goods(int goods_number=0,char*goods_name="null",char*person_name="null",int price=0,int amount=0)//定义构造函数
- {
- this->goods_number=goods_number;
- strcpy(this->goods_name,goods_name);
- strcpy(this->person_name,person_name);
- this->price=price;
- this->amount=amount;
- }
- void ShowData()//定义输出函数
- {
- cout<<"goods_number:"<<goods_number<<" goods_name:"<<goods_name<<" person_name:"<<person_name<<" price:"<<price<<" amount:"<<amount<<endl;
- }
- };
- int main()
- {
- Goods *goods=NULL;
- Goods *p;
- ifstream in("data.txt",ios::binary);
- if (! in.is_open())
- {
- cout << "Error opening file";
- exit (1);
- }
- Goods *p2;
- while (!in.eof())
- {
- if(goods==NULL)
- p2=p=goods=new Goods;
- else
- {
- p2=p;
- p->next=new Goods;
- p=p->next;
- }
- in.read((char*)&(*p),sizeof(*p));
- p->next=NULL;
- if(in.eof())
- {
- free(p2->next);
- p2->next=NULL;
- break;
- }//目的是清除最后一次重复计入的商品,这个操作和\n以及eof有关
- }
- p->next=NULL;
- p=goods;
- while(p)
- {
- p->ShowData();
- p=p->next;
- }//打印链表
- return 0;
- }
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <cstring>
class Goods
{
public:
char goods_name[50]; //商品名称
int goods_number; //商品代码
char person_name[30]; //经办人
int price; //进货价
int amount; //库存量
Goods *next;//定义指向类Goods的指针变量next
Goods(int goods_number=0,char*goods_name="null",char*person_name="null",int price=0,int amount=0)//定义构造函数
{
this->goods_number=goods_number;
strcpy(this->goods_name,goods_name);
strcpy(this->person_name,person_name);
this->price=price;
this->amount=amount;
}
void ShowData()//定义输出函数
{
cout<<"goods_number:"<<goods_number<<" goods_name:"<<goods_name<<" person_name:"<<person_name<<" price:"<<price<<" amount:"<<amount<<endl;
}
};
int main()
{
Goods *goods=NULL;
Goods *p;
ifstream in("data.txt",ios::binary);
if (! in.is_open())
{
cout << "Error opening file";
exit (1);
}
Goods *p2;
while (!in.eof())
{
if(goods==NULL)
p2=p=goods=new Goods;
else
{
p2=p;
p->next=new Goods;
p=p->next;
}
in.read((char*)&(*p),sizeof(*p));
p->next=NULL;
if(in.eof())
{
free(p2->next);
p2->next=NULL;
break;
}//目的是清除最后一次重复计入的商品,这个操作和\n以及eof有关
}
p->next=NULL;
p=goods;
while(p)
{
p->ShowData();
p=p->next;
}//打印链表
return 0;
}
商品表及链表,所选取的数据结构和实际需要应该还是很贴切的。