zoukankan      html  css  js  c++  java
  • 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);
  • 相关阅读:
    面向对象编程
    面向对象编程进阶
    pycharm常用快捷键
    面向对象
    深拷贝和浅拷贝
    hashlib模块
    日志配置
    常用模块大全
    正则详解
    软件目录规范
  • 原文地址:https://www.cnblogs.com/marky/p/3171596.html
Copyright © 2011-2022 走看看