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/fopen.tm.html
Hardcopy (PDF): A4 | Letter
AHELP for CIAO 3.4 fopen Context: slangrtl

Synopsis

Open a file

Syntax

File_Type fopen (String_Type f, String_Type m)

Description

The fopen function opens a file f according to the mode string m. Allowed values for m are:

     "r"    Read only
     "w"    Write only
     "a"    Append
     "r+"   Reading and writing at the beginning of the file.
     "w+"   Reading and writing.  The file is created if it does not
              exist; otherwise, it is truncated.
     "a+"   Reading and writing at the end of the file.  The file is created
              if it does not already exist.

In addition, the mode string can also include the letter 'b' as the last character to indicate that the file is to be opened in binary mode.

Upon success, fopen a File_Type object which is meant to be used in other operations that require an open file. Upon failure, the function returns NULL.

Example

The following function opens a file in append mode and writes a string to it:

    define append_string_to_file (file, str)
    {
       variable fp = fopen (file, "a");
       if (fp == NULL) verror ("%s could not be opened", file);
       () = fputs (string, fp);
       () = fclose (fp);
    }

Note that the return values from fputs and fclose are ignored.

There is no need to explicitly close a file opened with fopen. If the returned File_Type object goes out of scope, S-Lang will automatically close the file. However, explicitly closing a file after use is recommended.

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.