C++ 如何从C中的二进制文件中读取浮点数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1422817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:55:41  来源:igfitidea点击:

How to read a float from binary file in C?

c++cfile-io

提问by Polaris878

Everything I'm finding via google is garbage... Note that I want the answer inC, however if you supplement your answer with a C++ solution as wellthen you get bonus points!

一切我通过谷歌发现是垃圾......请注意,我想要的答案Ç,但是如果你用C ++解决方案补充你的答案,以及那么你获得积分!

I just want to be able to read some floats into an array from a binary file

我只想能够从二进制文件中将一些浮点数读入数组

EDIT: Yes I know about Endian-ness... and no I don't care how it was stored.

编辑:是的,我知道 Endian-ness ......不,我不在乎它是如何存储的。

回答by sth

How you have to read the floats from the file completely depends on how the values were saved there in the first place. One common way could be:

您必须如何从文件中读取浮点数完全取决于这些值最初是如何保存在那里的。一种常见的方法可能是:

void writefloat(float v, FILE *f) {
  fwrite((void*)(&v), sizeof(v), 1, f);
}

float readfloat(FILE *f) {
  float v;
  fread((void*)(&v), sizeof(v), 1, f);
  return v;
}

回答by Michael Krelin - hacker

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.

前提是它以兼容的二进制表示形式编写。

readfor file descriptors, freadfor FILE*s and istream::readfor c++iostreams. Pick whatever pleases you:

read文件描述符,fread对于FILE*S和istream::read用于c++输入输出流。选择你喜欢的任何东西:

read(fd,&f,sizeof(f))==sizeof(f)

fread(&f,sizeof(f),1,fp)==1

fin.read((char*)&f,sizeof(f)).gcount()==sizeof(f)

回答by vpram86

You could use fread. (Note the the API is for C, even though the website says C++ reference :))

你可以使用fread。(注意 API 是针对 C 的,尽管网站上写着 C++ 参考 :))

回答by Christoph

Use fread()from <stdio.h>. The assertions should be replaced with actual error handling code.

fread()从使用<stdio.h>。断言应替换为实际的错误处理代码。

#include <stdio.h>
#include <assert.h>

#define countof(ARRAY) (sizeof (ARRAY) / sizeof *(ARRAY))

float data[5];

FILE *file = fopen("foo.bin", "rb");
assert(file);

size_t n = fread(data, sizeof(float), countof(data), file);
assert(n == countof(data));

Keep in mind that you might run into endian issues if you transfer files between different architectures.

请记住,如果您在不同架构之间传输文件,您可能会遇到字节序问题。

回答by Daniel

If the file is all "float" and you wanted to read it X number of times, all you have to do is this:

如果文件都是“浮动的”,并且您想读取它 X 次,您所要做的就是:

FILE *fp;

if((fp=fopen("filename.whatever", "rb"))==NULL)
 return 0;

fseek(fp, 0, SEEK_END);
long size = ftell(fp);
fseek(fp, 0, SEEK_SET);

float *f = (float *)malloc(sizeof(float)*size);
if(f==NULL)
{
 fclose(fp);
 return 0;
}

if(fread(f, sizeof(float), size, fp)!=size)
{
 fclose(fp);
 return 0;
}

fclose(fp);

// do something with f

回答by aviraldg

FILE *thisFile=fopen("filename","fb");
float myFloat;
fscanf(thisFile,"%f",&myFloat);
fclose(thisFile);

This works if the data is written using fprintf (implementation specific)
However,you can also typecast your float to int32 and save , load and typecast.

如果使用 fprintf(特定于实现)写入数据,则此方法有效。
但是,您也可以将 float 类型转换为 int32 并保存、加载和类型转换。

std::fstream thisFile;
thisFile.open("filename",ios::read|ios::binary);
float myFloat;
thisFile>>myFloat;
thisFile.close();

May be wrong (I haven't used the C++ F.IO functions for a loooong loooong time)

可能是错误的(我已经很久没有使用 C++ F.IO 函数了)

回答by Charles

If these values are sequentially placed into a binary file you can do a read of sizeof(float) bytes per float value into a character array. You can then cast these into a float value.

如果这些值按顺序放入二进制文件中,您可以将每个浮点值的 sizeof(float) 字节读取到字符数组中。然后,您可以将这些转换为浮点值。