Mega Search
23.2 Million


Sign Up

Make a donation  
Why does 'fscanf' not working ?  
News Group: borland.public.cppbuilder.language.cpp


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!


Vote for best question.
Score: 0  # Vote:  0
Date Posted: 25-Dec-2007, at 7:19 AM EST
From: Daniel
 
Re: Why does 'fscanf' not working ?  
News Group: borland.public.cppbuilder.language.cpp
"Daniel"  wrote in message
news:47711f89$1@newsgroups.borland.com...

> 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.

> how do I use 'fscanf' for getting int values from a Binary file?

Like Alan() said, you have to use fread() instead.


Gambit



Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 25-Dec-2007, at 1:00 PM EST
From: Remy Lebeau \(TeamB\)
 
Re: Why does 'fscanf' not working ?  
News Group: borland.public.cppbuilder.language.cpp

>>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....



Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 25-Dec-2007, at 9:32 AM EST
From: Daniel
 
Re: Why does 'fscanf' not working ?  
News Group: borland.public.cppbuilder.language.cpp
"Daniel"  wrote:

>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
 Borland newsgroup descriptions
      netiquette

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 25-Dec-2007, at 3:22 PM EST
From: Alan Bellingham