fstream读写二进制文件

先从普通文件读数据,再将数据写到二进制文件,在将数据从二进制文件读出来,显示。
中间包括插入数据,删除数据,都是直接对文件操作。

涉及内容:标志位清除、读写文件指针重定位等


#ifndef HARDWARE_H_
#define HARDWARE_H_

#include <fstream>
using namespace std;

#define fileName "hardware.dat"
#define maxRecord 100

extern int count; 

struct Hardware
{
  int partNumber;
  char toolName[30];
  int quantity;
  float price;
};


void re_position(fstream &fd,int n,const int choice);
void Clear(fstream &fd);

void Initialization(fstream &fd);           //initializes the binary file
void Input(fstream &fd);                    //gets user's input information
void List(fstream &fd);                      //reads all records of tools from the binary file and displays the records on the screen
void Update(fstream &fd);                   //update the records
void Insert(fstream &fd);                   //insert a record in binary file
void Delete(fstream &fd);                   //delete a record in binary file

void Menu();
#endif



#include "hardware.h"
#include <fstream>
#include <iostream>
#include <string.h>
#include <iomanip>
using namespace std;

#define r 0
#define w 1
#define r0 2
#define w0 3

void re_position(fstream &fd,int n,const int choice)
{
  if(choice==r)    //read
  fd.seekg(n*sizeof(Hardware),ios::cur);
  else if(choice==w)            //write
   fd.seekp(n*sizeof(Hardware),ios::cur);
  else if(choice==r0)     //read
    fd.seekg(n*sizeof(Hardware));
  else                   //write
    fd.seekp(n*sizeof(Hardware));
}


void Clear(fstream &fd)
{
  fd.clear();  //clear eofbit,badbit,failbit
}



void Initialization(fstream &fd)
{
  int flag=0;              //0:not exist
  fd.open(fileName,ios::in);              
  if(!fd)
    {
      cout<<"File cannot be opened.Create a new empty file."<<endl;
      fd.close();
      fd.open(fileName,ios::out|ios::binary);   //create file 
    }
  else
    {
      flag=1;
      cout<<"Should the file be initialized(Y or N):";
    }
  fd.close();
  fd.open(fileName,ios::in|ios::out|ios::binary);   //open the file in binary mode,end up to close
  if(flag==0)
    {
      Hardware empty;
      cout<<"Enter the part number(0-99,-1 to end the input):";
      cin>>empty.partNumber;
      while(empty.partNumber>=0 &&empty.partNumber<100)
	{
	  cout<<"Enter the tool name:";
	  cin.get();                  //read the remain "enter"
	  cin.getline(empty.toolName,30);
	  cout<<"Enter the quantity:";
	  cin>>empty.quantity;
	  cout<<"Enter the price:";
	  cin>>empty.price;
	  fd.write(reinterpret_cast<char *>(&empty),sizeof(empty));
	  cout<<"Enter the part number(0-99,-1 to end the input):";
	  cin>>empty.partNumber;
	}
    }
  else
    {
      char temp;
      cin>>temp;
      if(temp=='Y' || temp=='y')
	{
	  Hardware empty;
	  empty.partNumber=-1;
	  empty.quantity=0;
	  empty.price=0;
	  for(int i=0;i<maxRecord;i++)
	    fd.write(reinterpret_cast<char *>(&empty),sizeof(empty));
	}
    }
}


void Input(fstream &fd)
{
  //fd.seekp(0);
  re_position(fd,0,w0);   //set the file pointer for writting
  Hardware temp;
  int tempNumber;
  cout<<"Enter the part number(0-99,-1 to end the input):";
  cin>>tempNumber;
  while(tempNumber>=0&&tempNumber<100)
    {
      temp.partNumber=tempNumber;
      cout<<"Enter the tool name:";
      cin.get();
      cin.getline(temp.toolName,30);
      cout<<"Enter the quantity:";
      cin>>temp.quantity;
      cout<<"Enter the price:";
      cin>>temp.price;
      fd.write(reinterpret_cast<char *>(&temp),sizeof(temp));
      cout<<"Enter the part number(0-99,-1 to end the input):";
      cin>>tempNumber;
      }
}


void List(fstream &fd)
{
  Clear(fd);
  re_position(fd,0,r0);//set the file pointer for reading
  Hardware temp;
  cout<<"Part#\tTool Name\t\t\tQuantity\tPrice"<<endl;
  fd.read(reinterpret_cast<char *>(&temp),sizeof(temp));
  while(temp.partNumber>=0 && temp.partNumber<100 && !fd.eof())
  //for(int i=0;i<10;i++)
  {
    cout<<setiosflags(ios::left)<<setw(4)<<temp.partNumber<<"\t"<<setiosflags(ios::left)<<setw(30)<<temp.toolName<<"\t"<<setiosflags(ios::left)<<setw(10)<<temp.quantity<<"\t"<<setiosflags(ios::left)<<temp.price<<endl;
    fd.read(reinterpret_cast<char *>(&temp),sizeof(temp));
  }
}


void Update(fstream &fd)
{
  Clear(fd);
  int input;
  Hardware temp;
  re_position(fd,0,r0);//set the file pointer for reading
  cout<<"Enter the part number for update:";
  cin>>input;
  fd.read(reinterpret_cast<char *>(&temp),sizeof(temp));
  while(temp.partNumber>=0 && temp.partNumber<100 && temp.partNumber!=input && !fd.eof())
    {
      fd.read(reinterpret_cast<char *>(&temp),sizeof(temp)); 
    }
  if(temp.partNumber!=input)
    cout<<"The record not exist."<<endl;
  else
    {
      re_position(fd,-1,w);
      cout<<"Enter the tool name:";
      cin.get();
      cin.getline(temp.toolName,30);
      cout<<"Enter the quantity:";
      cin>>temp.quantity;
      cout<<"Enter the price:";
      cin>>temp.price;
      fd.write(reinterpret_cast<char *>(&temp),sizeof(temp));
      cout<<"Record updated."<<endl;
    }
}



void Insert(fstream &fd)
{
  int input,count=0;
  Hardware temp;
  Clear(fd);
  re_position(fd,0,r0);//set the file pointer for reading 
  cout<<"Enter the part number for insertion:";
  cin>>input;
  fd.read(reinterpret_cast<char *>(&temp),sizeof(temp));
  while(temp.partNumber>=0 && temp.partNumber<100 && temp.partNumber<input && !fd.eof())
    {
      fd.read(reinterpret_cast<char *>(&temp),sizeof(temp));
      count++;
    }
  if(temp.partNumber==input)
    cout<<"The record already exist."<<endl;
  else if(fd.eof())
    {
      Clear(fd);
      temp.partNumber=input;
      re_position(fd,count,w0);
      cout<<"Enter the tool name:";
      cin.get();
      cin.getline(temp.toolName,30);
      cout<<"Enter the quantity:";
      cin>>temp.quantity;
      cout<<"Enter the price:";
      cin>>temp.price;
      fd.write(reinterpret_cast<char *>(&temp),sizeof(temp));
    }
  else
    {
      temp.partNumber=input;
      Hardware next1,next2;
      re_position(fd,-1,r);    //read pointer put forward
      fd.read(reinterpret_cast<char *>(&next1),sizeof(next1));
      re_position(fd,count,w0);
      cout<<"Enter the tool name:";
      cin.get();
      cin.getline(temp.toolName,30);
      cout<<"Enter the quantity:";
      cin>>temp.quantity;
      cout<<"Enter the price:";
      cin>>temp.price;
      fd.write(reinterpret_cast<char *>(&temp),sizeof(temp));
      count++;
      while(next1.partNumber>=0 && next1.partNumber<100 && !fd.eof())
	{
	  fd.read(reinterpret_cast<char *>(&next2),sizeof(next2));
	  re_position(fd,count,w0);
	  fd.write(reinterpret_cast<char *>(&next1),sizeof(next1));
	  next1.partNumber=next2.partNumber;
	  strcpy(next1.toolName,next2.toolName);
	  next1.quantity=next2.quantity;
	  next1.price=next2.price;
	  count++;
	}
      if(fd.eof())
	{
	  Clear(fd);
	  re_position(fd,count-1,w0);
	  fd.write(reinterpret_cast<char *>(&next1),sizeof(next1));
	}
      else
	{
	  re_position(fd,count,w0);
	  fd.write(reinterpret_cast<char *>(&next1),sizeof(next1));
	}
    }
  
}


void Delete(fstream &fd)
{
  Clear(fd);
  re_position(fd,0,r0);  //reposition to the first
  Hardware temp;         //store the reading from file
  int input,count=0;             //the number input
  cout<<"Enter the part number for deletion:";
  cin>>input;
  fd.read(reinterpret_cast<char *>(&temp),sizeof(temp));   //read the record
  while(temp.partNumber>=0 && temp.partNumber<100 && temp.partNumber<input && !fd.eof())
    {
      fd.read(reinterpret_cast<char *>(&temp),sizeof(temp));
      count++;
    }
  if(temp.partNumber!=input)
    cout<<"Cannot delete.The record is empty."<<endl;
  else
    {
      Hardware next;
      fd.read(reinterpret_cast<char *>(&next),sizeof(next));
      while(next.partNumber>=0&& next.partNumber<100)
	{
	  re_position(fd,count,w0);
	  fd.write(reinterpret_cast<char *>(&next),sizeof(next));
	  re_position(fd,count+2,r0);
	  fd.read(reinterpret_cast<char *>(&next),sizeof(next));
	  count++;
	}
      re_position(fd,count,w0);
      fd.write(reinterpret_cast<char *>(&next),sizeof(next));
      cout<<"Part#\tTool Name\t\t\tQuantity\tPrice"<<endl;
      cout<<setiosflags(ios::left)<<setw(4)<<temp.partNumber<<"\t"<<setiosflags(ios::left)<<setw(30)<<temp.toolName<<"\t"<<setiosflags(ios::left)<<setw(10)<<temp.quantity<<"\t"<<setiosflags(ios::left)<<temp.price<<endl;    
      cout<<"Record deleted."<<endl;
    }
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值