Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing long doubles to file 1

Status
Not open for further replies.

mrhegemon

Programmer
Jul 3, 2003
20
US
I would like to write the value of a long double variable to a file, but NOT in a formatted string. I want the raw 8 bytes of the number so I can keep all the precision without writing a 25-30 byte string representing the number. How is this done? Thanks for any help.
 
Try using fprintf and fscanf ...
u fprintf into the file with the desired format and read it up using fscanf..
sumthing like...
Code:
        long double  ld = //sum value;
	long double res = 0.0;
	FILE *fd = NULL;

        fd = fopen("c:/vs.txt", "w");
	if (fd)
	{
		fprintf(fd, "%lf", ld);
		fseek(fd, 0, SEEK_SET);
                fscanf(fd, "%lf", &res);
        }

I havent tried it out myself! dO Tell me whether its happening..

-vs
 
Code:
long double ld = 1.234;
FILE *fp = fopen( "doubles", "wb" );
if ( fp != NULL ) {
  if ( fwrite( &ld, sizeof(ld), 1, fp ) != 1 ) {
    perror("write");
  }
  fclose( fp );
} else {
  perror( "open" );
}
Bear in mind that binary files are much less portable than text files.
 
adds this code from Salems

long double ld;
FILE *fp;
if((fp = fopen(&quot;doubles&quot;, &quot;rb&quot;)) < 0)
{
printf(&quot;file open error.&quot;);
exit(1);
}

fseek(fp, 0, SEEK_SET);

fscanf(fp, &quot;%lf&quot;, &ld);
printf(&quot;\nMy long double value : %lf&quot;,ld);

fclose(fp);


fread is also useful...
if u want to read particular position with particular bytes
use fread();

ex:
fseek(fp, 0, SEEK_SET);
fread(&ld, size, 1, fp);

fseek uses fo place file pointer postions
here size is how many bytes u wants to read
& 1 is how many blocks u wants to read
 
> if((fp = fopen(&quot;doubles&quot;, &quot;rb&quot;)) < 0)
This is not how you check for open failure
fopen() returns NULL on failure, not an int < 0

> fscanf(fp, &quot;%lf&quot;, &ld);
This is not how you read a value written using fwrite()

Use
Code:
  if ( fread( &ld, sizeof(ld), 1, fp ) != 1 ) {
    perror(&quot;read&quot;);
  }
 
>This is not how you read a value written using fwrite()
i listed fread() below

>Use
if ( fread( &ld, sizeof(ld), 1, fp ) != 1 ) {
perror(&quot;read&quot;);
}

if we know the standard library function wat it is really doing
so why don't we use our own methods
 
Thank you so much, Salem. I was trying to use fwrite before but was having trouble because I was not passing the address of the variable. I know binary files are less portable, but they are better for the data analysis program I am making. Haha, and not to be mean, but you seem to be the only one here that knows what s/he is talking about.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top