newbie trouble reading long floating point

J

jimG

Hi, everyone,
I need to read a file with long (double precision) floating point
numbrers.
On the console, >od -N80 -tf8 -w32 a.bin reads the first ten numbers
correctly without any trouble.
The first number prints out as 7.50000000000000e+00, for example, which
is correct.
(The second one is 2.659273227531892e-20. And so on.)

My problem is trying to read them into a C program. I've tried all
kinds of stuff with fscanf and fread, but I can't get anything sensible
to read in. I've opened the file as rb with fopen. My best tries so
far:
float cc[8];
fscanf(fileptr,"%le",&cc);

or

fread(&cc,8,1,fileptr);

So far I am just trying to readin the first number.

Obviously, I'm doing something wrong.
I've read the faq, but I'm still lost.
Any help will be greatly appreciated.
TIA.
Jim G
 
S

SP

jimG said:
Hi, everyone,
I need to read a file with long (double precision) floating point
numbrers.
On the console, >od -N80 -tf8 -w32 a.bin reads the first ten numbers
correctly without any trouble.
The first number prints out as 7.50000000000000e+00, for example, which
is correct.
(The second one is 2.659273227531892e-20. And so on.)

My problem is trying to read them into a C program. I've tried all
kinds of stuff with fscanf and fread, but I can't get anything sensible
to read in. I've opened the file as rb with fopen. My best tries so
far:
float cc[8];
fscanf(fileptr,"%le",&cc);

or

fread(&cc,8,1,fileptr);

So far I am just trying to readin the first number.

Obviously, I'm doing something wrong.
I've read the faq, but I'm still lost.
Any help will be greatly appreciated.
TIA.
Jim G


a simple google search on fscanf, would bring you to a nice site
http://irc.essex.ac.uk/www.iota-six.co.uk/c/i4_fwrite_fscanf_fprintf.asp
which has the following sample code.

i = 0 ;
while(!feof(file)) {
/* loop through and store the numbers into the array */
fscanf(file, "%d", &numbers);
i++;
}

GOOGLE is your friend.
 
K

Keith Thompson

jimG said:
I need to read a file with long (double precision) floating point
numbrers.
On the console, >od -N80 -tf8 -w32 a.bin reads the first ten numbers
correctly without any trouble.
The first number prints out as 7.50000000000000e+00, for example, which
is correct.
(The second one is 2.659273227531892e-20. And so on.)

The Unix "od" command with the specified arguments reads 10 8-byte
binary floating-point numbers from a binary file. (Don't expect
everyone here to know that; we discuss C here, not Unix.)
My problem is trying to read them into a C program. I've tried all
kinds of stuff with fscanf and fread, but I can't get anything sensible
to read in. I've opened the file as rb with fopen. My best tries so
far:
float cc[8];
fscanf(fileptr,"%le",&cc);

or

fread(&cc,8,1,fileptr);

So far I am just trying to readin the first number.

Obviously, I'm doing something wrong.

But we don't know what. Show us a small, complete, self-contained C
program that illustrates the problem.

fscanf() reads text input, so it's not going to work with a binary
file.

Are you reading floats or double? The sizes of float and double are
implementation-defined, but float is typically 4 bytes; you're reading
8-byte quantities.

The first argument to fread() should be "cc", not "&cc". Since cc is
an array object, the expression is implicitly converted to a pointer
to the first element of the array. &cc is the address of the array.
It's not likely to make any difference (since it's implicitly
converted to void* anyway), but it's a good idea.

For the second argument, 8 is a magic number. Use sizeof, applied to
whatever type you're reading.
 
O

Old Wolf

SP said:
jimG said:
Hi, everyone,
I need to read a file with long (double precision) floating point
numbrers.

a simple google search on fscanf, would bring you to a nice site
http://irc.essex.ac.uk/www.iota-six.co.uk/c/i4_fwrite_fscanf_fprintf.asp
which has the following sample code.

i = 0 ;
while(!feof(file)) {
/* loop through and store the numbers into the array */
fscanf(file, "%d", &numbers);
i++;
}


a) This code is wrong.
b) This code doesn't even try to answer the original question.
GOOGLE is your friend.

If you like finding rubbish.
 
J

jimG

Thanks to all who replied, it turns out the simple fix was to change
the declaration from "float" to "double".
Jim Graber
Keith said:
jimG said:
I need to read a file with long (double precision) floating point
numbrers.
On the console, >od -N80 -tf8 -w32 a.bin reads the first ten numbers
correctly without any trouble.
The first number prints out as 7.50000000000000e+00, for example, which
is correct.
(The second one is 2.659273227531892e-20. And so on.)

The Unix "od" command with the specified arguments reads 10 8-byte
binary floating-point numbers from a binary file. (Don't expect
everyone here to know that; we discuss C here, not Unix.)
My problem is trying to read them into a C program. I've tried all
kinds of stuff with fscanf and fread, but I can't get anything sensible
to read in. I've opened the file as rb with fopen. My best tries so
far:
float cc[8];
fscanf(fileptr,"%le",&cc);

or

fread(&cc,8,1,fileptr);

So far I am just trying to readin the first number.

Obviously, I'm doing something wrong.

But we don't know what. Show us a small, complete, self-contained C
program that illustrates the problem.

fscanf() reads text input, so it's not going to work with a binary
file.

Are you reading floats or double? The sizes of float and double are
implementation-defined, but float is typically 4 bytes; you're reading
8-byte quantities.

The first argument to fread() should be "cc", not "&cc". Since cc is
an array object, the expression is implicitly converted to a pointer
to the first element of the array. &cc is the address of the array.
It's not likely to make any difference (since it's implicitly
converted to void* anyway), but it's a good idea.

For the second argument, 8 is a magic number. Use sizeof, applied to
whatever type you're reading.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top