关于链表的C++文件读写

关于链表的C++文件读写

出处http://blog.csdn.net/u013815546/article/details/42110269
标签: c++数据结构

课程设计时需要构建一个商品表,我使用的是一个链表来存储一个个商品,那么在过程中需要将信息存储进文件,再从文件读取到内存的链表中。

思路不是很难,但是还是卡在了一些细节上。

细节1:存储1~n个商品时,在最后一个商品及n商品时,在文件末尾会多一个换行符,这个换行符会影响到文件的读写操作。

细节2:链表的每个商品信息在读取时都需要动态分配内存,以及一些指针的操作问题。(平时只注重于算法的学习了,对于链表不熟悉吃的亏,在此感谢队友jameslee的Debug)

细节3:对于类的存储,我想到的是用二进制而不是一般存储,因为二进制是按照字节来存储的,方便文件读写,当然文件存储信息是一个过渡,学完数据库就方便了。

文件写入商品信息:

  1. #include <iostream.h>    
  2. #include <fstream.h>    
  3. #include <stdlib.h>  
  4. #include <cstring>  
  5. class Goods  
  6. {  
  7. protected:  
  8.     char goods_name[50]; //商品名称  
  9.     int goods_number; //商品代码  
  10.     char person_name[30]; //经办人  
  11.     int price; //进货价  
  12.     int amount; //库存量  
  13.     Goods *next;//定义指向类Goods的指针变量next  
  14. public:  
  15.     Goods(int goods_number=0,char*goods_name="null",char*person_name="null",int price=0,int amount=0)//定义构造函数  
  16.     {  
  17.         this->goods_number=goods_number;  
  18.         strcpy(this->goods_name,goods_name);  
  19.         strcpy(this->person_name,person_name);  
  20.         this->price=price;  
  21.         this->amount=amount;  
  22.     }  
  23.     void ShowData()//定义输出函数  
  24.     {  
  25.         cout<<"goods_number:"<<goods_number<<" goods_name:"<<goods_name<<" person_name:"<<person_name<<" price:"<<price<<" amount:"<<amount<<endl;  
  26.     }  
  27. };  
  28. int main ()   
  29. {  
  30.     ofstream out("data.txt",ios::app|ios::binary);  
  31.     Goods x(1,"name1","das",1,1);  
  32.     Goods y(2,"name2","das2",2,2);  
  33.     Goods z(3,"name3","das3",3,3);  
  34.     if (out.is_open())   
  35.     {  
  36.         out.write((char*)&x,sizeof(x));  
  37.         out.write((char*)&y,sizeof(y));  
  38.         out.write((char*)&z,sizeof(z));  
  39.     }  
  40.     return 0;  
  41. }  
#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;
}
文件读入内存的链表中:

  1. #include <iostream.h>    
  2. #include <fstream.h>    
  3. #include <stdlib.h>  
  4. #include <cstring>  
  5. class Goods  
  6. {  
  7.     public:  
  8.     char goods_name[50]; //商品名称  
  9.     int goods_number; //商品代码  
  10.     char person_name[30]; //经办人  
  11.     int price; //进货价  
  12.     int amount; //库存量  
  13.     Goods *next;//定义指向类Goods的指针变量next  
  14.     Goods(int goods_number=0,char*goods_name="null",char*person_name="null",int price=0,int amount=0)//定义构造函数  
  15.     {  
  16.         this->goods_number=goods_number;  
  17.         strcpy(this->goods_name,goods_name);  
  18.         strcpy(this->person_name,person_name);  
  19.         this->price=price;  
  20.         this->amount=amount;  
  21.     }  
  22.     void ShowData()//定义输出函数  
  23.     {  
  24.         cout<<"goods_number:"<<goods_number<<" goods_name:"<<goods_name<<" person_name:"<<person_name<<" price:"<<price<<" amount:"<<amount<<endl;  
  25.     }  
  26. };  
  27. int main()   
  28. {    
  29.    Goods *goods=NULL;  
  30.    Goods *p;  
  31.    ifstream in("data.txt",ios::binary);    
  32.    if (! in.is_open())  
  33.    {   
  34.        cout << "Error opening file";  
  35.        exit (1);   
  36.    }  
  37.    Goods *p2;  
  38.    while (!in.eof())  
  39.    {  
  40.          
  41.         if(goods==NULL)  
  42.             p2=p=goods=new Goods;  
  43.         else  
  44.         {  
  45.             p2=p;  
  46.             p->next=new Goods;  
  47.             p=p->next;  
  48.         }  
  49.         in.read((char*)&(*p),sizeof(*p));  
  50.         p->next=NULL;  
  51.         if(in.eof())  
  52.         {  
  53.             free(p2->next);  
  54.             p2->next=NULL;  
  55.             break;  
  56.         }//目的是清除最后一次重复计入的商品,这个操作和\n以及eof有关  
  57.    }  
  58.    p->next=NULL;  
  59.    p=goods;  
  60.    while(p)  
  61.    {  
  62.     p->ShowData();  
  63.     p=p->next;  
  64.    }//打印链表  
  65.    return 0;  
  66. }  
#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;
}
商品表及链表,所选取的数据结构和实际需要应该还是很贴切的。

posted @ 2017-05-17 18:33  tsvico  阅读(1154)  评论(0编辑  收藏  举报