delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/10/28/22:18:08

Date: Tue, 28 Oct 1997 19:16:49 -0800 (PST)
Message-Id: <199710290316.TAA12824@adit.ap.net>
Mime-Version: 1.0
To: saf AT club-internet DOT fr, djgpp AT delorie DOT com
From: Nate Eldredge <eldredge AT ap DOT net>
Subject: Re: fputs(); HELP !!! SOS

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 -


  webmaster     delorie software   privacy  
  Copyright 2019   by DJ Delorie     Updated Jul 2019