473,385 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Read input from file

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


Nov 14 '05 #1
4 1551


Ronny Mandal wrote:
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.)

--
Er*********@sun.com

Nov 14 '05 #2
Ronny Mandal wrote:
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;
}

---
Al Bowers


Nov 14 '05 #3
In article <d3**********@news1brm.Central.Sun.COM>,
Eric Sosman <er*********@sun.com> wrote:
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?
--
Any sufficiently old bug becomes a feature.
Nov 14 '05 #4
>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
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Gunnar | last post by:
Hello, I've just written a CPP program that reads integers from a binary file, and used this code while (my_ifstram.read( (char* ) &number, sizeof(int)) { // do something with number } My...
18
by: jas | last post by:
Hi, I would like to start a new process and be able to read/write from/to it. I have tried things like... import subprocess as sp p = sp.Popen("cmd.exe", stdout=sp.PIPE)...
3
by: Bill Cohagan | last post by:
I'm writing a console app in c# and am encountering a strange problem. I'm trying to use redirection of the standard input stream to read input from a (xml) file. The following code snippet is from...
10
by: Tibby | last post by:
I need to read/write not only text files, but binary as well. It seems like on binary files, it doesn't right the last 10% of the file. -- Thanks --- Outgoing mail is certified Virus...
8
by: a | last post by:
I have a struct to write to a file struct _structA{ long x; int y; float z; } struct _structA A; //file open write(fd,A,sizeof(_structA)); //file close
1
by: Jose Reckoner | last post by:
I'm running python 2.3 on Windows XP. Anyone have a quick small script to convert .DT1 and .DEM data to ASCII or some other format? I don't need a viewer. Thanks!
8
by: kepioo | last post by:
I currently have an xml input file containing lots of data. My objectiv is to write a script that reports in another xml file only the data I am interested in. Doing this is really easy using SAX....
6
by: portCo | last post by:
Hello there, I am creating a vb application which is some like like a questionare. Application read a text file which contains many questions and display one question and the input is needed...
9
by: vineeth | last post by:
Hello all, I have come across a weird problem, I need to determine the amount of bytes read from a file, but couldn't figure it out , My program does this : __ file = open("somefile") data =...
63
by: Bill Cunningham | last post by:
I don't think I can do this without some help or hints. Here is the code I have. #include <stdio.h> #include <stdlib.h> double input(double input) { int count=0,div=0; double...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.