C++における構造体データのファイル読み書き

3711 ワード

構造体にはデータの種類がたくさんあります.
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;
	}
};

一般的なストレージ方式を推奨するのではなく、バイナリ読み書きを採用する必要があります.これにより、読み書き操作、特に読み取り操作が便利になります.
バイナリメモリからファイルへの書き込み:
#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 () 
{  
   Goods x,y,z;
   ifstream in("data.txt",ios::binary);  
   if (! in.is_open())  
   { 
	   cout << "Error opening file";
	   exit (1); 
   }
   while (!in.eof() )
   {  
       in.read((char*)&x,sizeof(x));
	   in.read((char*)&y,sizeof(y));
	   in.read((char*)&z,sizeof(z));
   }
   x.ShowData();
   y.ShowData();
   z.ShowData();
   return 0;  
}
注バイナリに格納ファイルdata.txtは、開くと文字化けして記憶機能としてしか使用されませんが、VCは自分で認識できますし、他のバイナリを表示できるファイルツールで表示することもできます.
コンパイル環境:VC++6.0
用途:カリキュラム設計