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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading long doubles from a binary file

Status
Not open for further replies.

mrhegemon

Programmer
Jul 3, 2003
20
US
Okay, I was able to get my program to correctly write the raw value of my variable to the file. I am now trying to make another program to allow me to convert the binary data file to a formatted string file just in case I would like to have the data human-readable. Unfortunately, I am getting a strange error when using fread, and I believe my syntax is correct. As a note, the file I am reading from was written with another program using the following snippet of code where avintensity was defined as a long double and outfile was fopened as a "wb" file:

[tt]fwrite(&avintensity,8,1,outfile);[/tt]

Here is the code for my conversion program:

[tt]#include <stdio.h>

int main(int argc,char* argv[])
{
FILE* infile=fopen(argv[1],&quot;rb&quot;);
FILE* outfile=fopen(argv[2],&quot;w&quot;);
long double tmp=0;

while(!feof(infile))
{
fread(&tmp,8,1,infile);
fprintf(outfile,&quot;% .10Lf\n&quot;,tmp);
}

fclose(infile);
fclose(outfile);
return 0;
}[/tt]

And this is the error I get after compiling, linking, and executing:

[tt]> convert data6.dat data6c.dat
convert: no delegate for this image format (data6.dat) [No such file or directory].
convert: Missing an image file name.[/tt]

Thanks for any help.
 
> FILE* infile=fopen(argv[1],&quot;rb&quot;);
> FILE* outfile=fopen(argv[2],&quot;w&quot;);
You should check these for errors
if ( infile == NULL || outfile == NULL ) { /* error code */ }

> while(!feof(infile))
feof() does not mean what you think it means. Basically, if you have a 10 byte file, and read 10 bytes, feof() is still FALSE. You have to try and read the 11th byte in order to make feof() return true. This invariably means that you make one extra pass through the loop. feof() reads a status flag which is set when a file reading operation fails. It is not set immediately after the last byte of the file is read.

> fread(&tmp,8,1,infile);
Don't use literal constants for the size, use sizeof(tmp)
Additionally, the status result of each file reading function should be checked.

> fprintf(outfile,&quot;% .10Lf\n&quot;,tmp);
There is a space in the format string - this isn't good.
I'm guessing that the undefined conversion &quot;% &quot; causes the unexpected error messages.


The body of the code should be
Code:
while ( (fread(&tmp,sizeof(tmp),1,infile)) == 1 ) {
    fprintf(outfile,&quot;%.10Lf\n&quot;,tmp);
}
 
Sorry, none of those suggestions helped because I finally realized that there is a utility on the computer I am programming on called &quot;convert&quot; and it was executing instead of my program and would not accept the arguments I gave it.

I ended up using your suggestion for my original program, of not using literals, and that is where the problem arose. I was writing 8 bytes of a long double which on this Linux machine appears to be 12 bytes long. It works perfectly now that I use sizeof(), thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top