I'm trying to read two data (different types) from binary file (First I had written these data in it). I have problem with output. it's really unexpected:
0.000000000000000000000000 and 10000.
Where's the problem in reading double data type?
(OS: win xp; compiler tc 3.0; similar result i get with Visual C++ 6.0- result is -96745678999908980978989789 and 10000)

the code is the following:

#include <stdio.h>

enum {SUCCESS, FAIL};
struct dataForWrite
	{
		double DNum;
		int INum;
	};

int ErrorMsg(char *str);
void DataWrite(FILE *fout, struct dataForWrite *sd);
void DataRead(FILE *fin, char *string);

int main( )
{
	int reval = SUCCESS;

	FILE *fptr;
	char filename[] = "strnum1.mix";
	struct dataForWrite x; 

	x.DNum = 123.45;
	x.INum = 10000;	
	
	if ((fptr = fopen(filename, "wb+")) == NULL)
		{
			reval = ErrorMsg(filename);
			
		}
	else
		{
			DataWrite(fptr, &x);
			rewind(fptr);
			DataRead(fptr, filename);
			fclose(fptr);
		}
	return reval;
}

int ErrorMsg(char *str)
{
	printf("cannot open the string %s\n", str);
	
	return FAIL;
}
void DataWrite(FILE *fout, struct dataForWrite *sd)
{
	printf("Ready for writting\n");
	printf("writting the following data: %f	%d\n", (*sd).DNum, (*sd).INum);
	fprintf(fout, "%f	%d", (*sd).DNum, (*sd).INum);

}
void DataRead(FILE *fin, char *string)
{
	double fnum;
	int inum;

	printf("ready for reading from the following file: %s\n", string);

	fscanf(fin, "%f		%d", &fnum, &inum);
	printf("reading the following data: %f			%d\n", fnum, inum);
	
}

Recommended Answers

All 5 Replies

It's not a binary file if you're using fprintf/fscanf to access the file. All the "b" mode will do is turn off any end of line translations, but since you never write a newline, this isn't an issue.

Further, you don't check the return result of fscanf(). You should. If it fails, you'll print garbage (and different garbage from one compiler to the next).
As in

if ( fscanf(fin, "%f %d", &fnum, &inum) == 2 ) {
  printf("reading the following data: %f %d\n", fnum, inum);
} else {
}

A single white space in a scanf call matches any amount of whitespace on the input.

3. Between writing and reading of a stream opened for update, you need to do fflush(fptr);

commented: It's been too long since I've added to your considereable rep. +14

if ( fscanf(fin, "%lf %d", &fnum, &inum) == 2 ) { If you are reading into a double you need to let fscanf know by appending a l ( that's the letter `L' ) between % and f if ((fptr = fopen(filename, "wb+")) == NULL) That will erase any data the file contains. Or in another words it will create an empty file.
"r+" is probably what you want.

void DataRead(FILE *fin, char *string)
{
	double fnum;
	int inum;

	printf("ready for reading from the following file: %s\n", string);

	fscanf(fin, "%f		%d", &fnum, &inum);
	printf("reading the following data: %f			%d\n", fnum, inum);
}

In case you haven't noticed. The variables fnum and inum will be gone as soon as the function is done. You are not doing more than displaying the values, however if you ever wanted to do something else with those values, they will not be available after the function is finished.

Thanks people! I appreciate Your help. I still have to learn. And I've few more question:
1) what function should I've use instead of fprintf for writting data (different types) in binary file? Is the fwrite function the right answer? If the answer is yes, how should we properly do the writting in the file with this function (maybe with some loop) cause this function is writting block of data which sharing the same type?
2) I have not used ever before the function fflush, and I don't know how to use it (properly). I'll find some explanations about it on the net.

I'm still learning syntax of this language (and obviously) I have some problem with it, but also I'm planning to improve pragmatical knowledge. I appreciate if You recommend me a book for this.
p.s.
And Aia thanks for telling me for unproper use of reading into double...

>>1) what function should I've use instead of fprintf for writting data (different types) in binary file? Is the fwrite function the right answer?
~Yes.
If you want to write generic code than conformed to the standards, fwrite() is your function when binary data is concerned.

>>If the answer is yes, how should we properly do the writting in the file with this function (maybe with some loop) cause this function is writting block of data which sharing the same type?
~Yes.
Some sources for further explanation and examples:
Example
Knock yourself out

>>2) I have not used ever before the function fflush, and I don't know how to use it (properly). I'll find some explanations about it on the net.
~The standard output stream is buffered, meaning that whatever the programmer intends to show to the screen ( as standard ) or to a file, it will not happen until it gets the ok for doing so.
Using the function fflush() ensures that it will flash it, writing to output when you intend.
A example of this will be not using a /n at the end of string when writing printf()
i.e.

printf( "Enter your name: " ); /* I don't want the cursor to return to another line, thus not /n found at end */
fflush( stdout ); /* Will ensure that there will be a prompt asking for name at screen */

Granted, many compilers will try to do the right thing and it will prompt the user for a name
without the need of fflush() but is not a guarantied.

At times you'll see this: fflush( stdin ); This is an aberration of the use of fflush() function. Never do that. fflush accepts an output stream as argument and not an input stream.

>>I'm still learning syntax of this language (and obviously) I have some problem with it, but also I'm planning to improve pragmatical knowledge. I appreciate if You recommend me a book for this.
~No book author pays me enough, for me to recommend any of their books. Therefore I will not endorse any of them. ;)
However I can pass you some links to useful resources and sometimes recommendations from other people.
Starting C
Starting out in C

>>And Aia thanks for telling me for unproper use of reading into double...
~Salem showed you the most important part. Check the return of your functions.

commented: I wandered by and noticed you're still doing good deeds. Here's some rep. +14

Thanks! Problem solved!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.