9

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? Commented 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. Commented 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??? Commented Sep 14, 2009 at 20:19
  • Yeah sorry I don't have every C API memorized -_- Commented Sep 15, 2009 at 23:00

7 Answers 7

20

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;
}
Sign up to request clarification or add additional context in comments.

8 Comments

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.
Mind that the byte order does matter here if you move the file across platforms.
I believe anything x86 or x64 will have the same endianness... correct?
What would I have to do if I wanted to read a fixed number of floats from th file? Thanks
@Polaris878: Call that function a fixed number of times?
|
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)

Comments

3

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

1 Comment

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

Comments

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

Comments

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 Comment

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.