7

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

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

4
  • 1
    In what format are these floats stored, exactly? Sep 14, 2009 at 17:16
  • 6
    This is why I love StackOverflow... I just wait a couple of minutes and I already have 5 answers that are better than anything else out there. Sep 14, 2009 at 17:28
  • You don't care about how the floats are stored, and yet you need someone to tell you something as basic as how to read from a binary file??? Sep 14, 2009 at 20:19
  • Yeah sorry I don't have every C API memorized -_- Sep 15, 2009 at 23:00

7 Answers 7

19

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;
}
8
  • 1
    Thank you, I don't know why the hell it was so hard for everyone else I was finding on google to write that simple statement. I swear to God people just like making things more complicated for the hell of it. Sep 14, 2009 at 17:22
  • 2
    Mind that the byte order does matter here if you move the file across platforms.
    – xtofl
    Sep 14, 2009 at 17:27
  • I believe anything x86 or x64 will have the same endianness... correct? Sep 14, 2009 at 17:29
  • What would I have to do if I wanted to read a fixed number of floats from th file? Thanks Sep 14, 2009 at 18:15
  • 4
    @Polaris878: Call that function a fixed number of times?
    – sth
    Sep 14, 2009 at 18:22
5
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)
3

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

1
  • 4
    Bear in mind that depending on whether the float was written by a machine with different endian-ness then you may have to reorder the bytes after reading.
    – Jherico
    Sep 14, 2009 at 17:16
3

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

#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.

2

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

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
1
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.

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)

1
  • O.P. asked for files in binary format. This gives files in Ascii, which is easier to read, but not what was asked for. Sorry.
    – DragonLord
    Apr 5, 2012 at 7:27
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.