Link to home
Start Free TrialLog in
Avatar of cb068
cb068

asked on

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.

   
 
Avatar of akshayxx
akshayxx
Flag of United States of America image

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
Avatar of MN_GMan
MN_GMan

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);

Avatar of cb068

ASKER

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
Can you give a hex dump of the file?
Avatar of cb068

ASKER

Here it is,

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

is the hex equivalent of the number 2.1
ASKER CERTIFIED SOLUTION
Avatar of MN_GMan
MN_GMan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cb068

ASKER

Thanks MN GMan....it worked fine.
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.