| |||
| Home > RealView Debugger Predefined Macros > Alphabetical predefined macro reference > fread | |||
Reads the contents of a file into a buffer.
unsigned long fread (buffer, count, size, fileid)
void *buffer;
unsigned count;
unsigned size;
int fileid;
where:
bufferSpecifies the start of the area into which the data is written.
countSpecifies the number of elements.
sizeSpecifies the size of each element in bytes.
fileidThe ID number of the file containing the data to
be read. This must be a user-defined .fileid
This macro reads the contents of a file into a buffer. You
must define the identity of the file with either the fopen macro or
the FOPEN command.
unsigned
longThe size of the data that is
read, and is the same as size * count. However, it
returns the value -1 if an end-of-file (EOF)
or error has occurred.
This example shows how to use fread in
a macro:
Create an INCLUDE file
containing the following macro, for example, c:\myincludes\myfile.inc:
define /R void readFile(nElements)
int nElements;
{
char buffer[37];
int nbytes;
int recLen;
recLen = 6;
if (nElements > recLen)
error(2,"Enter a number from 1 to %d.\n",recLen);
else {
strcpy(buffer,"One \nTwo \nThree\nFour \nFive \nSix \n");
fopen(100,"c:\\myfiles\\data.txt","w");
nbytes = fwrite(buffer, nElements, recLen, 100);
$printf "%d bytes written\n",nbytes$;
fclose(100);
fopen(100,"c:\\myfiles\\data.txt","r");
memset(buffer,0,37);
nbytes = fread(buffer, nElements, recLen, 100);
$printf "%d bytes read\n",nbytes$;
$printf "Strings:\n%s",buffer$;
fclose(100);
}
}
.
At the command line, include the file you created in step 1:
include 'c:\myincludes\myfile.inc'
Run the macro, specifying a value from 1 to 6:
readFile(4)