How can I write __float128 data type to binary file?

146 views Asked by At

I am using quadmath library for quad-precision. Can I use fwrite function to write to binary file?

struct rec
{ 
   __float128 mydata;
}

struct rec my_record;

mydata=1.41421356237309504880q;
fwrite(&my_record, sizeof(struct rec), 1, myfile);
1

There are 1 answers

2
dbush On

You should have no problem writing to a file. As long as you read it back in the same way (i.e. with fread) and the file isn't copied between machines you should be fine.

You also don't need to encapsulate the value in a struct to do this:

__float128 mydata;
int rval;

mydata=1.41421356237309504880q;

rval = fwrite(&mydata, sizeof(mydata), 1, myfile);
if (rval < 1) {
    perror("failed to write record");
} 

...

rval = fread(&mydata, sizeof(mydata), 1, myfile);
if (rval < 1) {
    perror("failed to read record");
}