Board index » cppbuilder » Why does 'fscanf' not working ?

Why does 'fscanf' not working ?


2007-12-25 10:19:37 PM
cppbuilder67
Hi all, when I'm running the following code -
int Result, MyInt = 0;
char MyChar = 0;
FILE *fp = fopen( "MyFile.bin", "rb" );
fseek( fp, 0, SEEK_SET );
Result = fscanf( fp, "%c", &MyChar );
fclose( fp );
Then everything OK, the command 'fscanf' works OK, and I get
Result = 1, and MyChar = 0x53, but when I replace the fscanf
with this line -
Result = fscanf( fp, "%d", &MyInt );
and I run the application, then I get Result = 0, and MyInt = 0,
why does it not working? how do I use 'fscanf' for getting int
values from a Binary file?
"MyFile.bin" file is a binary file that looks like this -
0x53 0x87 0xA2 0xD8 .....
Thanks!
 
 

Re:Why does 'fscanf' not working ?

"Daniel" < XXXX@XXXXX.COM >wrote:
Quote
why does it not working? how do I use 'fscanf' for getting int
values from a Binary file?
You don't. You use fread(). fscanf() is for reading text.
Alan Bellingham
--
Team Browns
<url:www.borland.com/newsgroups/>Borland newsgroup descriptions
<url:www.borland.com/newsgroups/netiquette.html>netiquette
 

Re:Why does 'fscanf' not working ?

Quote
>why does it not working? how do I use 'fscanf' for getting int
>values from a Binary file?

You don't. You use fread(). fscanf() is for reading text.

Alan Bellingham
That strange! but Thanks....
 

{smallsort}

Re:Why does 'fscanf' not working ?

"Daniel" < XXXX@XXXXX.COM >wrote in message
Quote
when I replace the fscanf with this line -

Result = fscanf( fp, "%d", &MyInt );

and I run the application, then I get Result = 0,
and MyInt = 0, why does it not working?
Because the ...scanf() functions are designed for parsing textual data, not
binary data.
Quote
how do I use 'fscanf' for getting int values from a Binary file?
Like Alan() said, you have to use fread() instead.
Gambit