By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,687 Members | 2,046 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,687 IT Pros & Developers. It's quick & easy.

saving arrays of floats to a file

P: n/a
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[i]);
}

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/

*/
Sep 4 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Yu SONG wrote:
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[i]);
}

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.
Sep 4 '06 #2

P: n/a

"Yu SONG" <ti**@mi6.gov.ukwrote in message
news:ed**********@wisteria.csv.warwick.ac.uk...
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[i]);
}

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[i]);
needs this space to seperate the file contents so
they can be read back.

Regards

Sep 5 '06 #3

P: n/a

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.


Yu SONG wrote:
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[i]);
}

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/

*/
Sep 5 '06 #4

P: n/a
markpapadakis wrote:
>
There is almost always a 'better' way to do everything.

Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or:
<http://www.caliburn.nl/topposting.html>


Brian (there's certainly a better way of posting!)
Sep 5 '06 #5

P: n/a
Yu SONG wrote:
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[i]);
}

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[i]);' in order to get the
full range and precision of the float.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Sep 5 '06 #6

P: n/a
Joe Wright <jo********@comcast.netwrites:
Yu SONG wrote:
>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[i]);
}
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[i]);' 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]);

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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 5 '06 #7

P: n/a
Keith Thompson wrote:
Joe Wright <jo********@comcast.netwrites:
[ snippage ]
>I would change to 'fprintf(fp, "%.8e\n", src[i]);' 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]);

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.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Sep 7 '06 #8

P: n/a
Joe Wright <jo********@comcast.netwrites:
Keith Thompson wrote:
>Joe Wright <jo********@comcast.netwrites:

[ snippage ]
>>I would change to 'fprintf(fp, "%.8e\n", src[i]);' 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]);
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?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 7 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.