saving arrays of floats to a file

Y

Yu SONG

Hi all,

What would be the most efficient way to save an array of floats to a
file (in text format)?

At the moment, my code looks like:


/*
* Saving an array of floats to a file
*/


void save(char* filename, float* src, int limit) {

FILE* fp = fopen(filename, "w");

if (fp != NULL) {

register int i;
for (i=0; i<limit; i++) {
fprintf(fp,"%.2f ", src);
}

fclose(fp);

} else {
printf("Error: Saving data !!!");
}

}


Is there a better way to do this? Thanks



--
Yu Song

/* E-mail.c */

#define User "Yu.Song"
#define At '@'
#define Host "warwick.ac.uk"

int main() {
printf("Yu Song's E-mail: %s%c%s", User, At, Host);
return 0;}

/*

Further Info. : http://www.dcs.warwick.ac.uk/~yu/

*/
 
T

Tim Prince

Yu said:
Hi all,

What would be the most efficient way to save an array of floats to a
file (in text format)?

At the moment, my code looks like:


/*
* Saving an array of floats to a file
*/


void save(char* filename, float* src, int limit) {

FILE* fp = fopen(filename, "w");

if (fp != NULL) {

register int i;
for (i=0; i<limit; i++) {
fprintf(fp,"%.2f ", src);
}

fclose(fp);

} else {
printf("Error: Saving data !!!");
}

}


Is there a better way to do this? Thanks

You haven't divulged your (or your instructor's) definition of
efficiency or "better," so almost any answer would do. In many
circumstances, it might be more efficient to put multiple values per
line, or to use a format which is not so restricted in the representable
range.
 
T

Terry

Yu SONG said:
Hi all,

What would be the most efficient way to save an array of floats to a file
(in text format)?

At the moment, my code looks like:


/*
* Saving an array of floats to a file
*/


void save(char* filename, float* src, int limit) {

FILE* fp = fopen(filename, "w");

if (fp != NULL) {

register int i;
for (i=0; i<limit; i++) {
fprintf(fp,"%.2f ", src);
}

fclose(fp);

} else {
printf("Error: Saving data !!!");
}

}


Is there a better way to do this? Thanks


Your method looks fine to me.
!
The statement fprintf(fp,"%.2f ", src);
needs this space to seperate the file contents so
they can be read back.

Regards
 
M

markpapadakis

There is almost always a 'better' way to do everything.

For instance, you could unroll the loop ( or even use Duff's Device for
the fun of it ), avoid using text files and save the data as raw (
whereas you don't actually need a loop for storing them ) etc.
 
J

Joe Wright

Yu said:
Hi all,

What would be the most efficient way to save an array of floats to a
file (in text format)?

At the moment, my code looks like:


/*
* Saving an array of floats to a file
*/


void save(char* filename, float* src, int limit) {

FILE* fp = fopen(filename, "w");

if (fp != NULL) {

register int i;
for (i=0; i<limit; i++) {
fprintf(fp,"%.2f ", src);
}

fclose(fp);

} else {
printf("Error: Saving data !!!");
}

}


Is there a better way to do this? Thanks


I would change to 'fprintf(fp, "%.8e\n", src);' in order to get the
full range and precision of the float.
 
K

Keith Thompson

Joe Wright said:
Yu said:
What would be the most efficient way to save an array of floats to a
file (in text format)?
At the moment, my code looks like:
/*
* Saving an array of floats to a file
*/
void save(char* filename, float* src, int limit) {
FILE* fp = fopen(filename, "w");
if (fp != NULL) {
register int i;
for (i=0; i<limit; i++) {
fprintf(fp,"%.2f ", src);
}
fclose(fp);
} else {
printf("Error: Saving data !!!");
}
}
Is there a better way to do this? Thanks


I would change to 'fprintf(fp, "%.8e\n", src);' in order to get the
full range and precision of the float.


And how do you know that "%.8e" gives you the full range and precision
of type float? (It probably does on most platforms.)

Something like this might be more reliable:

fprintf(fp, "%.*e\n", FLT_DIG, src);

I'm not certain that FLT_DIG is adequate; you might want FLT_DIG + 1
or FLT_DIG + 2.

Possibly FLT_DIG should be replaced by (int)FLT_DIG; it's likely that
the expansion of FLT_DIG is a constant expression of type int, but I
don't see anything in the standard that would forbid defining FLT_DIG
as, say, 6L.
 
J

Joe Wright

Keith said:
Joe Wright <[email protected]> writes:

[ snippage ]
I would change to 'fprintf(fp, "%.8e\n", src);' in order to get the
full range and precision of the float.


And how do you know that "%.8e" gives you the full range and precision
of type float? (It probably does on most platforms.)

Something like this might be more reliable:

fprintf(fp, "%.*e\n", FLT_DIG, src);

I'm not certain that FLT_DIG is adequate; you might want FLT_DIG + 1
or FLT_DIG + 2.

Possibly FLT_DIG should be replaced by (int)FLT_DIG; it's likely that
the expansion of FLT_DIG is a constant expression of type int, but I
don't see anything in the standard that would forbid defining FLT_DIG
as, say, 6L.


I haven't found FLT_DIG (or DBL_DIG for that matter) provides useful
information.

I have determined by inspection that float requires an 8 in that
position and double requires 16.

Unhappily, on my iron (DJGPP, x86), FLT_DIG is 6 and DBL_DIG is 15.
These are also 'implementation detail' I think, and not required, one
way or another.

I would encourage data transfer among disparate system hardware to be in
text format.

Most of us can figure out your text format by looking at it, without
even asking you about it.

If you send us a binary stream that we don't already know exquisitely
well, data communication between us has stopped and your phone is ringing.
 
K

Keith Thompson

Joe Wright said:
Keith said:
Joe Wright <[email protected]> writes:

[ snippage ]
I would change to 'fprintf(fp, "%.8e\n", src);' in order to get the
full range and precision of the float.

And how do you know that "%.8e" gives you the full range and
precision
of type float? (It probably does on most platforms.)
Something like this might be more reliable:
fprintf(fp, "%.*e\n", FLT_DIG, src);
I'm not certain that FLT_DIG is adequate; you might want FLT_DIG + 1
or FLT_DIG + 2.
Possibly FLT_DIG should be replaced by (int)FLT_DIG; it's likely that
the expansion of FLT_DIG is a constant expression of type int, but I
don't see anything in the standard that would forbid defining FLT_DIG
as, say, 6L.


I haven't found FLT_DIG (or DBL_DIG for that matter) provides useful
information.

I have determined by inspection that float requires an 8 in that
position and double requires 16.

Unhappily, on my iron (DJGPP, x86), FLT_DIG is 6 and DBL_DIG is
15. These are also 'implementation detail' I think, and not required,
one way or another.


Really?

Here's what the standard says about FLT_DIG (C99 5.2.4.2.2p9):

number of decimal digits, q, such that any floating-point number
with q decimal digits can be rounded into a floating-point number
with p radix b digits and back again without change to the q
decimal digits,

p * log10(b) if b is a power of 10
floor((p-1) * log10(b))) otherwise

(I've tweaked the mathematical notation used in the standard, I hope
correctly.)

Does the value FLT_DIG == 6 on your platform not meet those
requirements?
 

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,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top