?
Solved

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
8
Medium Priority
?
231 Views
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[])
    {
     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.

   
 
0
Comment
Question by:cb068
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 8

Expert Comment

by:akshayxx
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;
fscanf("%lf",&d);

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

Expert Comment

by:MN_GMan
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);

0
 

Author Comment

by:cb068
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
0
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!

 

Expert Comment

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

Author Comment

by:cb068
ID: 8126412
Here it is,

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

is the hex equivalent of the number 2.1
0
 

Accepted Solution

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

0
 

Author Comment

by:cb068
ID: 8126575
Thanks MN GMan....it worked fine.
0
 

Expert Comment

by:vijay_rangaraj
ID: 8126665
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

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays 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

752 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