DPRG List  

[DPRG] C programming question - reading binary files

Subject: [DPRG] C programming question - reading binary files
From: Dave Hylands dhylands at broadcom.com
Date: Fri Sep 17 02:36:17 CDT 2004

Hi Clay,

What's the type of 'val'? Fscanf wants it to be declared as char or
unsigned char, not int.

I also noticed that you're not testing the return value of fscanf. It
returns the number of arguments successfully parsed. If fscanf returns 0
then 'val' will be uninitialized.

As somebody else suggested, I'd use fgetc instead (since you're only
interested in one character at a time). Scanf/fscanf are banned in any
code I work on because of their pathetic error handling and recovery
ability. I tend to use fgets/sscanf instead.

The 'b' parameter will only be meaningful on DOS/Windows platforms, and
it prevents \n from being translated to \r\n when writing the file and
prevents \r\n being translated into \n when reading the file.

Dave Hylands
Vancouver, BC, Canada

> -----Original Message-----
> From: dprglist-bounces at dprg.org 
> [mailto:dprglist-bounces at dprg.org] On Behalf Of Clay Timmons
> Sent: Thursday, September 16, 2004 4:21 PM
> To: dprglist at dprg.org
> Subject: [DPRG] C programming question - reading binary files
> Working frantically on my robot...
> I'm trying to get my robot's voice working again.  Dusting 
> off old code
> that worked years ago.
> I'm having a problem reading a binary file.  Here is the code snippit.
> fp = fopen(filename,"rb");   /* open for read of binary file */
> while(!feof(fp)) {
> 	fscanf(fp,"%c",&val);
>    ...
> }
> This works fine on every PC I've used except the new one onboard my
> robot.
> I'm reading a sound file, wave format.  I read the 8 bit samples and
> dump them out the parallel port to a DAC (poor mans sound card).
> The problem I'm seeing is that all values read back are zero. 
> Also there
> is a read failure at the end of file; ABORT, RETRY, IGNORE.
> I suspect my problem may be caused by using a DiskOnChip for a hard
> drive.
> Not sure how well the Flash File System supports binary files.
> My questions...
> fopen - How is open binary different than a regular open?  
> "rb" vs "r"?
> Reading from binary file - is fscanf the best way or should I 
> use fgetc?
> Both seem to give the same results.
> Regards,
> -Clay-
> _______________________________________________
> DPRGlist mailing list
> DPRGlist at dprg.org
> http://list.dprg.org/mailman/listinfo/dprglist

More information about the DPRG mailing list