C 读写二进制文件

FILE  *ffp;
    struct mystruct
    {
        int  aa;
        int  bb;
    };
    struct head
    {
        float cc;
        float dd;
    };
    if((ffp=fopen("D:\aaa.dat","ab+"))!=NULL)
    {
        head myhead;
        myhead.cc=123.55;
        myhead.dd=789.11;
        fwrite(&myhead,sizeof(head),1,ffp);
        vector<mystruct> temp;
        for(int i=0;i<3;i++)
        {
            mystruct my;
            my.aa=i+2;
            my.bb=i+3;
            temp.push_back(my);
        }
        
        fwrite(&temp[0],sizeof(mystruct),3,ffp);
        fclose(ffp);
    }

    FILE *frd =fopen("D:\aaa.dat","rb+");
       head gethead;
     //  fread(&gethead,sizeof(head),1,frd);
     //  float mycc=gethead.cc;
     //  TCHAR sz[20];
     //   _stprintf(sz,_T("%.3f
"),mycc);
        //OutputDebugString(sz);
       int hsize=sizeof(head);
     //  fread(&gethead,sizeof(head),1,frd);
        fseek(frd,hsize,0);
     
        // *****一次性全部读出数据
        mystruct temp2[3];
        fread(&temp2[0],sizeof(mystruct),3,frd);
        for(int l=0;l<3;l++)
        {
           int ff=temp2[l].aa;  
          TCHAR szz[20];
          _stprintf(szz,_T("%d,%d
"),ff,temp2[l].bb);
            OutputDebugString(szz);
        }

        //*******每次读一个数据
     // for(int i=0;i<3;i++)
     // {
        //  mystruct temp2;
        //  fread(&temp2,sizeof(mystruct),1,frd);
        //  int ff=temp2.aa;  
        //  TCHAR sz[20];
  //      _stprintf(sz,_T("%d"),ff);
        //OutputDebugString(sz);
     // }
      fclose(frd);
原文地址:https://www.cnblogs.com/marky/p/3171596.html