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

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

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

细节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
{
	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;
}
商品表及链表,所选取的数据结构和实际需要应该还是很贴切的。


  • 12
    点赞
  • 73
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++中,可以使用链表来读取文件。下面是一个示例代码,演示了如何使用链表读取文件中的数据: ```cpp #include <iostream> #include <fstream> #include <string> struct Node { std::string data; Node* next; }; void readFileToList(Node*& head, const std::string& filename) { std::ifstream file(filename); if (file.is_open()) { std::string line; while (std::getline(file, line)) { Node* newNode = new Node; newNode->data = line; newNode->next = nullptr; if (head == nullptr) { head = newNode; } else { Node* current = head; while (current->next != nullptr) { current = current->next; } current->next = newNode; } } file.close(); } else { std::cout << "Unable to open file: " << filename << std::endl; } } void printList(const Node* head) { const Node* current = head; while (current != nullptr) { std::cout << current->data << std::endl; current = current->next; } } int main() { Node* head = nullptr; std::string filename = "example.txt"; readFileToList(head, filename); printList(head); // 释放链表内存 Node* current = head; while (current != nullptr) { Node* temp = current; current = current->next; delete temp; } return 0; } ``` 这段代码首先定义了一个链表节点结构体`Node`,其中包含一个字符串数据成员`data`和一个指向下一个节点的指针`next`。 然后,`readFileToList`函数用于读取文件并将每一行数据存储到链表中。它首先打开文件,然后逐行读取文件内容。对于每一行,它创建一个新的节点,并将行数据存储到节点的`data`成员中。然后,它将新节点添加到链表的末尾。 最后,`printList`函数用于遍历链表并打印每个节点的数据。 在`main`函数中,我们首先定义了一个空链表头指针`head`,然后指定要读取的文件名`filename`。接下来,我们调用`readFileToList`函数将文件内容读取到链表中,并调用`printList`函数打印链表中的数据。 请注意,在使用完链表后,我们需要手动释放链表节点的内存,以避免内存泄漏。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值