如何从二进制文件读取和写入浮点数? [英] How to read and write float numbers from and into binary files?

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

问题描述

程序应如何使用C或Vala语言在二进制文件中读取和写入浮点数?

How should a program read and write float numbers from and into binary files in C or Vala language?

进行写和读的通用API通常被设计为以字节格式写.我的意思是您必须将一字节数据数组写入文件并以相同格式读取.

The common APIs to do writing and reading are generally designed to write in byte format. I mean you have to write arrays of one-byte data into file and read in the same format.

我正在寻找一种以浮点格式书写和阅读的方法.无需类型转换,也不必将数字更改为字符串.有可能吗?

I'm looking for a way to write and read in float format. without typecasting and without having to change the number into string. Is it possible?

推荐答案

fwrite()和fread()或write()和read()可以正常工作.

fwrite() and fread() or write() and read() will work just fine.

float da, db ;
    ...
fwrite( &da, 1, sizeof(da), fpout ) ;
    ...
fread( &db, 1, sizeof(db), fpin ) ;

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

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