About Chandra Archive Proposer Instruments & Calibration Newsletters Data Analysis HelpDesk Calibration Database NASA Archives & Centers Chandra Science Links

Skip the navigation links
Last modified: December 2006

URL: http://cxc.harvard.edu/ciao3.4/fread.tm.html
Hardcopy (PDF): A4 | Letter
AHELP for CIAO 3.4 fread Context: slangrtl

Synopsis

Read binary data from a file

Syntax

UInt_Type fread (Ref_Type b, DataType_Type t, UInt_Type n, File_Type fp)

Description

The fread function may be used to read n objects of type t from an open file pointer fp. Upon success, it returns the number of objects read from the file and places the objects in the variable specified by b. Upon error or end of file, it returns -1. If more than one object is read from the file, those objects will be placed in an array of the appropriate size. The exception to this is when reading Char_Type or UChar_Type objects from a file, in which case the data will be returned as an n character BString_Type binary string, but only if n>1.

Example

The following example illustrates how to read 50 bytes from a file:

     define read_50_bytes_from_file (file)
     {
        variable fp, n, buf;

        fp = fopen (file, "rb");
        if (fp == NULL) error ("Open failed");
        n = fread (&buf, Char_Type, 50, fp);
        if (n == -1)
        error ("fread failed");
        () = fclose (fp);
        return buf;
     }

Use the pack and unpack functions to read data with a specific byte-ordering.

Hardcopy (PDF): A4 | Letter
Last modified: December 2006



The Chandra X-Ray Center (CXC) is operated for NASA by the Smithsonian Astrophysical Observatory.
60 Garden Street, Cambridge, MA 02138 USA.    Email: cxcweb@head.cfa.harvard.edu
Smithsonian Institution, Copyright © 1998-2004. All rights reserved.