473,320 Members | 2,104 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,320 software developers and data experts.

saving arrays of floats to a file

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
8 4127
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

"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

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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Duncan Smith | last post by:
I am having problems saving arrays to file. e.g. >>> t1 array() >>> t2 array() >>> t3 array() >>> t4 array(,
7
by: G-Factor | last post by:
Hi all I've just started learning about saving files. I got bit of a problem. The following code gives me an error about incompatible types. (Cannot covert from class character to char *). I...
1
by: Helmut Blass | last post by:
hi Access-freaks, in MS Access, I generate an Excel file where the user should have the possibility to determine the excel file name by file dialog. so I use the following command: ...
1
by: M. D'Costa | last post by:
Hello, I am saving a csv file through an ASP.NET web application. The data is obtained from a database and saved to the local users machine. I am using the Response objects methods of...
2
by: M. D'Costa | last post by:
Hello, I am saving a csv file through an ASP.NET web application. The data is obtained from a database and saved to the local users machine. I am using the Response objects methods of...
1
by: Bill Maher | last post by:
I have an array that I use to write to the file on the C:\ drive. I want to print the file. How can I do it. Here is my code for the write to the file: I'm using "writeline". To allow for...
1
by: Lespaul36 | last post by:
I have an icon structure..so it is a structure with other structures in it. I need to save it to a binary file. I have done some googling..but not much luck. How can I do this? Also, will long...
4
by: bei | last post by:
Hi, I am trying to write several arrays into one file, with one arrays in one column. Each array (column) is seperated by space. ie. a= b= c= 1 5 9 2 6 10 3 7 11 4 8 12
0
by: a | last post by:
hallo vs 2005 here on saving an htm file with a custom encoding (from the little arrow on the Save button in the file save dialog window) how to have the iso-8859-1 encoding ? I'm in...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.