如何读取二进制文件在C浮动? [英] How to read a float from binary file in C?

查看:172
本文介绍了如何读取二进制文件在C浮动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一切我通过谷歌发现垃圾...请注意,我想要的答案的 C ,但是如果你有一个C补充你的答案++解决方案<强>以及,然后你会得到加分!

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++ solution as well then you get bonus points!

我只是希望能够从二进制文件中读取一些彩车到一个数组

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

编辑:是的,我知道字节序...没有我不关心它是如何存储。

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

推荐答案

您怎么也得读彩车从文件完全取决于价值是如何在第一时间保存在那里。一种常见的方式可以是:

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;
}

这篇关于如何读取二进制文件在C浮动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆