C++->10.3.6.设有两个按升序排列的二进制文件a和b,将他们合并成一个新的升序二进制数据文件file。

#include<iostream.h>

#include<stdlib.h>

#include<string.h>

#include<fstream.h>

struct staff{

     long staNum;

     char staName[500];

     long phoNum;

     long num;

     char staAddr[1000];

}staf,staf1;   

void main(){  

       fstream fstr1,fstr2,fstr,fstrt;

      char i,fn1[80],fn2[80],fn[80],fnt[80],*p1,*p2,*p,*pt,str[1000];

     //staff staf1;

    int a,b;

    p1=&fn1[0]; p2=&fn2[0]; p=&fn[0]; pt=&fnt[0];

    cout<<"Please input the file name of fn1:\n";

     //cin>>p1;    

    //fstr1.open(p1,ios::binary|ios::in|ios::out);     //1

    fstr1.open("E:\\新建文件夹\\新建文件夹\\a.bat",ios::binary|ios::in|ios::out);

    if(!p1){ cerr<<"Can't open the file fn1.\n"; return; }

    fstr1.seekp(0,ios::end);

    cout<<"Please input the number i(Y|y).\n";

    cin>>i;

    while(i=='Y'||i=='y'){

    cout<<"Please input the number staf.\n";

    cin>>staf.staNum>>staf.staName>>staf.phoNum>>staf.num>>staf.staAddr;

    fstr1.write((char *) &staf,sizeof(staf));    //文件名对象.write函数里的buffer类型只能是(char *),不能是其他类型fstr1.write((int *) &a,sizeof(a));

    cout<<"Please input the number i.(Y|y)\n";

    cin>>i;  }

    cout<<"Please input the file name of fn2:\n";

    //cin>>p2;

    //fstr2.open(p2,ios::binary|ios::in|ios::out);   //2

    fstr2.open("E:\\新建文件夹\\新建文件夹\\b.bat",ios::binary|ios::in|ios::out);

    if(!p2){ cerr<<"Can't open the file fn2.\n"; return; }

    fstr2.seekp(0,ios::end);

    cout<<"Please input the number i.(Y|y)\n";

    cin>>i;

    while(i=='Y'||i=='y'){

    cout<<"Please input the number staf1.\n";

    cin>>staf1.staNum>>staf1.staName>>staf1.phoNum>>staf1.num>>staf1.staAddr;

    fstr2.write((char *) &staf1,sizeof(staf1));

    cout<<"Please input the number i(Y|y).\n"; cin>>i;  }

    cout<<"Please input the file name of fn.\n";    //3    

    //cin>>p;

    //fstr.open(p,ios::binary|ios::in|ios::out);

     fstr.open("E:\\新建文件夹\\新建文件夹\\file.bat",ios::binary|ios::in|ios::out);

     if(!p){ cerr<<"Can't open the file of fn.\n"; return; }

     fstr1.seekp(0,ios::end);

     long pEnd1=fstr1.tellp();

     fstr2.seekp(0,ios::end);

     long pEnd2=fstr2.tellp();

      fstr1.seekp(0,ios::beg);

      fstr2.seekp(0,ios::beg);

      while(fstr1.tellp()!=pEnd1&&fstr2.tellp()!=pEnd2){

      fstr1.read((char *) &staf,sizeof(staf));

      fstr2.read((char *) &staf1,sizeof(staf1));

      if(staf.staNum>>staf1.staNum) fstr.write((char *) &staf,sizeof(staf));

      else fstr.write((char *) &staf1,sizeof(staf1));

       //fstr<<staf1.staNum<<ends<<staf1.staName<<ends<<staf1.phoNum<<ends<<staf1.num<<staf1.staAddr;     } 

       while(fstr1.tellp()!=pEnd1) fstr.write((char *) &staf,sizeof(staf));

       while(fstr2.tellp()!=pEnd2) fstr.write((char *) &staf1,sizeof(staf1));

       cout<<"Please input the file name of fn4.\n";    //4

        //cin>>pt;

        //fstrt.open(pt,ios::in|ios::out);

          fstrt.open("E:\\新建文件夹\\新建文件夹\\l.txt",ios::in|ios::out);

          if(!pt){ cerr<<"Can't open the file of fn.\n"; return; }

          fstr.seekg(0,ios::end);

         long pEnd3=fstr.tellg();

         fstr.seekg(0,ios::beg);

         while(fstr.tellg()!=pEnd3){

         fstr.read((char *) &staf,sizeof(staf));

         fstrt<<staf.staNum<<ends<<staf.staName<<ends<<staf.phoNum<<ends<<staf.num<<staf.staAddr<<endl;    

         cout<<staf.staNum<<ends<<staf.staName<<ends<<staf.phoNum<<ends<<staf.num<<staf.staAddr<<endl;

         } cout<<endl; fstrt.close();

         cout<<"Please input the file name of fn4.\n";    //5

         //cin>>pt;

         //fstrt.open(pt,ios::in|ios::out);

          fstrt.open("E:\\新建文件夹\\新建文件夹\\l.txt",ios::in|ios::out);

          if(!pt){ cerr<<"Can't open the file of fn.\n"; return; }

          //while(fstrt.tellg()!=ptEnd){ fstrt.getline(fn,800);

          //cout<<staf.staNum<<ends<<staf.staName<<ends<<staf.phoNum<<ends<<staf.num<<staf.staAddr<<endl; } //二进制的判断方法    

          do{ fstrt.getline(str,1000); cout<<str<<endl;  }while(!fstrt.eof());

          fstr1.close(); fstr2.close(); fstr.close(); fstrt.close();

} */

posted @ 2019-11-11 10:17  博客_在线  阅读(505)  评论(0)    收藏  举报