Question: I have noticed that I can't read binary data correctly from a file when I use the FREAD() function contained within the C I/O routines in the RTS.LIB.

Why ?

Answer: The FREAD() routine does an 8 bit read, in LOW BYTE, HIGH BYTE order, when used on a input file. That is to say if you were to try to do the following:
fread(&sample1, sizeof(int), 1, fpInputFile); fread(&sample2, sizeof(int), 1, fpInputFile);
And fpInputFile pointed to a file containing BINARY data such as: 0x1234 0x5678 etc.. sample1 would = 0x0034, and sample2 would equal 0x0012 AFTER both FREADs were completed.

The documentation within the Optimizing C Compiler User"s Guide is in error. Page 4-5 explains that both a CHAR and an INT are treated as 16 bit numbers on the C54x. This is the root of the problem. The workaround then is do a double FREAD() and concatenate the two 8 bit words read in the following manner: Assuming you wished to read a 16 bit integer value from PC memory such as 0x1234, into the variable SAMPLE you would do the following:

int SAMPLE = 0;
int low = 0;
int high = 0;

fread(&low, 1, 1, fpInputFile);
fread(&high, 1, 1, fpInputFile);

SAMPLE = ( (high << 8) | low ); 

The outcome of said operations will be: low="0x0034" high="0x0012" SAMPLE="0x1234"

 


Device: TMS320C5xx
Category: TI Tools
Detail: Code Generation Tools
Detail2: Compiler
Title: Can't Read Binary Data
Source: Case from the TMS320 Hotline
Date: 4/25/98
GenId: 30021

© Copyright 1998 Texas Instruments Incorporated. All rights reserved.
Trademarks, Important Notice!