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(FileNotFoundExceptio n 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.
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
}
catch(FileNotFoundExceptio
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.
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);
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);
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
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?
ASKER
Here it is,
@ ¨d¨d¨d¨d¨d¨T
is the hex equivalent of the number 2.1
@ ¨d¨d¨d¨d¨d¨T
is the hex equivalent of the number 2.1
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
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