• C

Accessing a double number in a file from C (the double is written to file from Java with writeDouble method)

I have a java program that uses the writeDouble() method of RandomAccessFile class and writes a double number to an o/p file. Now, I want to access that file from C side and retrieve the double number. How do I do it? (fscanf,fread etc donot seem to work).

This is the Java code

import java.io.*;
class gendouble{
    public static void main(String args[])
    {
     try
         {
          File gen=new File("\\gen");
          RandomAccessFile filehandle=new RandomAccessFile(gen,"rw");
                filehandle.writeDouble(102.123);
         }
     catch(FileNotFoundException ex){}
     catch(IOException ex){}
    }
}

Now, how do I retrieve the value 102.123 that has been written to the o/p file named gen from C side?

Thank you.

   
 
cb068Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

akshayxxCommented:
how does ur file look like .. is it writing in binary mode or text mode ?

can u see the number as it is if u open it in notepad ? if yes then its text mode

if it is in text mode here is what u can do on c side


FILE *fp=fopen("filename","r");
double d;
fscanf("%lf",&d);

i hope u know rest of the stuff of a simple c program
0
MN_GManCommented:
If the number is binary, this may work (assuming both the java and C use the same binary standard for storing doubles)

Another possibility might be a big endian / little endian conflict if stored in binary format.

Assuming same standard and same endian format is used, you should be able to use fread.



double     buffer;

fread(&buffer, sizeof(double), 1, fp);

0
cb068Author Commented:
I'm afraid fread is not working and as for your question akshay, it writes as a binary and hence not readable from C side.
I tried out fscanf,fread and I even tried retrieving character by character...but unfortunately they are both not compatible as far as the format is concerned. There is conflict in Big endian little endian as well as problem with Unicode and Ascii
0
Become an IT Security Management Expert

In today’s fast-paced, digitally transformed world of business, the need to protect network data and ensure cloud privacy has never been greater. With a B.S. in Network Operations and Security, you can get the credentials it takes to become an IT security management expert.

MN_GManCommented:
Can you give a hex dump of the file?
0
cb068Author Commented:
Here it is,

@ ¨d¨d¨d¨d¨d¨T

is the hex equivalent of the number 2.1
0
MN_GManCommented:
If the number is binary, Unicode/ASCII have no effect since those are ways of storing text.  

The standards I was referring to are IEEE standards for binary floating points.  I believe ANSI C uses IEEE 754 for example.

That is a text dump. A hex dump would look something like:
1D 5A 64 3B DF 87 59 40
which I believe is a little endian storage of 102.123 using ANSI standard C.


To fix only a big endian/little endian problem try the following code:

char       buf[8];
double     d;
char      *temp;


FILE *fp=fopen("\\gen","r");

//read in the value.
fread(&buf[0], sizeof(double), 1, fp);

//now swap the bytes.
temp = (char *)&d;
temp[0] = buf[7];
temp[1] = buf[6];
temp[2] = buf[5];
temp[3] = buf[4];
temp[4] = buf[3];
temp[5] = buf[2];
temp[6] = buf[1];
temp[7] = buf[0];

//print it
printf("%f\n", d);

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cb068Author Commented:
Thanks MN GMan....it worked fine.
0
vijay_rangarajCommented:
Hi,
     The data is not written to file as it is!! By default, prior to writing the data to file, the value is converted into long by using DoubleToLongBits method available in the Double Class.

     So, what is written to the file is not readable by a double data type in C

      The data is written in the following format:

       bit 63 - sign
       bit 62-52 - exponent
       bit 51-0 - Mantissa

       So, we need to write our C code to understand data in this format.!!
Regards
-- Vijay Rangaraj.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.