using C - FILE (stdio.h)

  • 文件的类型: 二进制文件/一般文件, 只是读取和写入的方式不一样罢了

    • 读取一般文件: (写类似)

      FILE *fi;
      fi=fopen("15.dat","r");
      
    • 读取二进制文件: (写类似)

      FILE *fi;
      fi=fopen("15.dat","rb");
      
  • 读数据: (写类似)

    • 数据规整的话 (均为同一类型) 可以全部读出, Like:

      FILE * fi2;
      float * data2 =new float[length*width*heigth];
      fi2=fopen("1.bin","rb");
      fread(data2, sizeof(float), length*width*heigth, fi2);
      
    • 不规整的话: Like:

      float x,y,z,t,f;
      //get to the real data point, start reading
      do
      {
          res = fscanf(fi, "%f %f %f %f %f", &x, &y, &z, &t, &f);
      } while (res != EOF && res == 5);
      

    NOTE: 写用

    fprintf(fo, "%f\t%f\t0.0\n", a, b);
    

    fwrite (x, 1, sizeof(float) , fo );
    

特别注意: fprintf 对应fscanf, fwrite对应fread, 别搞混了!!!

  • 忽略前面的说明信息, 找到真正的开始点再读取:

    //look for a starting point
    do
    {
        res=fscanf(fi,"%100s", stub);
    } while (res != EOF && stricmp(stub, "aaaaa")!=0);
    

    注: aaaaa为你真正数据的上一个信息, 下一个就为真实数据

using C++ - fstream (fstream.h)

cc: http://www.newxing.com/Tech/Program/Cpp/577.html