Read input from file

R

Ronny Mandal

Hi.

Assume you have a file with the format

1.2 4.2
4.3 2.9

i.e

double[space]double
double[space]double
....

Then assume you want to read this with a c-program.

fscanf(ifile, "%lf%lf", &x, &y); where ifile is a file-pointer will do the
job.

But if you want to read binary, with e.g fread, is there a way to read the
two variables the same way? Or do I have to do something like this:


fread( ( void* ) &temp, sizeof( temp ), 1, ifile );
x = temp;
fread( ( void* ) &temp, sizeof( temp ), 1, ifile );
y=temp;

where x,y and temp are double.

--

Thanks in advance,


Ronny Mandal
 
E

Eric Sosman

Ronny said:
Hi.

Assume you have a file with the format

1.2 4.2
4.3 2.9

i.e

double[space]double
double[space]double
...

Then assume you want to read this with a c-program.

fscanf(ifile, "%lf%lf", &x, &y); where ifile is a file-pointer will do the
job.

But if you want to read binary, with e.g fread, is there a way to read the
two variables the same way? Or do I have to do something like this:


fread( ( void* ) &temp, sizeof( temp ), 1, ifile );
x = temp;
fread( ( void* ) &temp, sizeof( temp ), 1, ifile );
y=temp;

where x,y and temp are double.

You could begin by eliminating `temp' (and the
unnecessary casts):

fread (&x, sizeof x, 1, ifile);
fread (&y, sizeof y, 1, ifile);

Alternatively, you could use an array of two
`double' values instead of (or in addition to) the
two independent variables:

double array[2];
fread (array, sizeof array[0], 2, ifile);
/* there are cleverer ways to write `2' here */
x = array[0]; /* if desired */
y = array[1]; /* if desired */

Think twice before you do this sort of thing,
though, because it will create Trouble With A Capital
T And That Rhymes With P And That Stands For -- er,
that stands for "data exchange." If you ever want to
create such a binary file on one machine and read it on
another, you will find yourself limited to exchanges
between machines that happen to represent `double'
values identically. (And yes: even machines that use
IEEE floating-point disagree about how the bits of their
floating-point numbers are arranged.)
 
A

Al Bowers

Ronny said:
> Hi.
>
> Assume you have a file with the format
>
> 1.2 4.2
> 4.3 2.9
>
> i.e
>
> double[space]double
> double[space]double
> ...
>
> Then assume you want to read this with a c-program.
>
> fscanf(ifile, "%lf%lf", &x, &y); where ifile is a file-pointer will do the job.
>
> But if you want to read binary, with e.g fread, is there a way to
read the two variables the same way? Or do I have to do something like this:
>
>
> fread( ( void* ) &temp, sizeof( temp ), 1, ifile );
> x = temp;
> fread( ( void* ) &temp, sizeof( temp ), 1, ifile );
> y=temp;
>
> where x,y and temp are double.
>

Make temp an array of two doubles.
Example:

#include <stdlib.h>
#include <stdio.h>

int main(void)
{
double d1[2] = {1.2,4.2},d2[2] = {4.3,2.9},new[2];
int i;
FILE *fp;

if((fp = fopen("temp.b","wb")) == NULL)
exit(EXIT_FAILURE);
fwrite(d1,sizeof d1,1,fp);
fwrite(d2,sizeof d2,1,fp);
fclose(fp);
/* Test */
if((fp = fopen("temp.b","rb")) == NULL)
exit(EXIT_FAILURE);
for(i = 0; i < 2;i++)
{
if(1 == fread(new,sizeof new, 1,fp))
printf("new[0] = %.2f new[1] = %.2f\n",
new[0],new[1]);
}
fclose(fp);
remove("temp.b");
return 0;
}
 
W

Walter Roberson

If you ever want to
create such a binary file on one machine and read it on
another, you will find yourself limited to exchanges
between machines that happen to represent `double'
values identically. (And yes: even machines that use
IEEE floating-point disagree about how the bits of their
floating-point numbers are arranged.)

I know there was a time, not long past, when different major Windows
compilers used different (incompatable) floating point formats.
Out of curiosity, would anyone know if that's still happening? Or
did the market finally shake out for Windows floating point?
 
G

Gordon Burditt

I know there was a time, not long past, when different major Windows
compilers used different (incompatable) floating point formats.
Out of curiosity, would anyone know if that's still happening? Or
did the market finally shake out for Windows floating point?

I strongly suspect that since it's almost impossible (if not actually
impossible) to buy a modern i386 architecture machine WITHOUT
hardware floating point on the processor, the hardware format has
been adopted. This wasn't the case in the days of the 386/486
CPUs.

Gordon L. Burditt
 

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
474,161
Messages
2,570,892
Members
47,431
Latest member
ElyseG3173

Latest Threads

Top