3.2.13. fread

Reads the contents of a file into a buffer.

Syntax

unsigned long fread (buffer, count, size, fileid)
void *buffer;
unsigned count;
unsigned size;
int fileid;

where:

buffer

Specifies the start of the area into which the data is written.

count

Specifies the number of elements.

size

Specifies the size of each element in bytes.

fileid

The ID number of the file containing the data to be read. This must be a user-defined fileid.

Description

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.

Return value

unsigned long

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

Rules

None

Example

This example shows how to use fread in a macro:

  1. 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);
      }
    }
    .
    
  2. At the command line, include the file you created in step 1:

    include 'c:\myincludes\myfile.inc'
    
  3. Run the macro, specifying a value from 1 to 6:

    readFile(4)
    

See also

Copyright © 2002-2009 ARM Limited. All rights reserved.ARM DUI 0175K
Non-Confidential