Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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

Posted on 2003-03-13
Medium Priority
Last Modified: 2010-08-05
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[])
          File gen=new File("\\gen");
          RandomAccessFile filehandle=new RandomAccessFile(gen,"rw");
     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.

Question by:cb068

Expert Comment

ID: 8126208
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;

i hope u know rest of the stuff of a simple c program

Expert Comment

ID: 8126276
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);


Author Comment

ID: 8126306
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 8126338
Can you give a hex dump of the file?

Author Comment

ID: 8126412
Here it is,

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

is the hex equivalent of the number 2.1

Accepted Solution

MN_GMan earned 100 total points
ID: 8126472
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);


Author Comment

ID: 8126575
Thanks MN GMan....it worked fine.

Expert Comment

ID: 8126665
     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.!!
-- Vijay Rangaraj.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question