How to read a float from binary file in C?

前端 未结 7 723
挽巷
挽巷 2020-12-10 17:31

Everything I\'m finding via google is garbage... Note that I want the answer in C, however if you supplement your answer with a C++ solutio

7条回答
  •  抹茶落季
    2020-12-10 17:51

    float f;
    if(read(fd,&f,sizeof(f))==sizeof(f))
        printf("%f\n",f);
    else
        printf("oops\n");
    

    Provided that it's written as compatible binary representation.

    read for file descriptors, fread for FILE*s and istream::read for c++ iostreams. Pick whatever pleases you:

    read(fd,&f,sizeof(f))==sizeof(f)
    
    fread(&f,sizeof(f),1,fp)==1
    
    fin.read((char*)&f,sizeof(f)).gcount()==sizeof(f)
    

提交回复
热议问题