Mail Archives: djgpp/1997/10/28/22:18:08
At 12:40 10/27/1997 +0100, Stephan wrote:
>Hello the world !
>
>I have a problem:
>How can I write a numeric value to the disk ?
>when I make:
>..
>FILE *disk;
>float i;
>
>...
>
>
>fputs(i,disk); IT DOESNT WORK CORRECTLY
>fclose(disk);
>
>
>How can I write a numeric value to the disk ?
You have two options when writing data to files. You can write it in text
format, or in binary. For text format:
The advantage is that any other program can read your data, and it will
always work on any platform.
FILE *f;
float i = 12345.0;
f = fopen("datafile","wt"); /* open in text mode */
...
fprintf(f,"%.5g\n",i); /* print with 5 decimal places and followed by newline */
...
fclose(f);
Binary format is usually more compact, but can't be read by other programs
and may not work on some machines.
FILE *f;
float i = 54321.0;
f = fopen("datafile","wb"); /* open in binary mode! important! */
...
fwrite(&i,sizeof(float),1,f); /* write the appropriate number of bytes */
...
fclose(f);
Nate Eldredge
eldredge AT ap DOT net
- Raw text -