x04 Chapter Contents
x04 Chapter Introduction
NAG C Library Manual

# NAG Library Function Documentnag_open_file (x04acc)

## 1  Purpose

nag_open_file (x04acc) opens a file for reading, writing or appending, and returns an associated file identifier.

## 2  Specification

 #include #include
 void nag_open_file (const char *filename, Integer mode, Nag_FileID *fileid, NagError *fail)

## 3  Description

nag_open_file (x04acc) opens a file for reading, writing or appending and returns an associated file identifier. This function is intended for use by callers of certain functions in Chapters e04 and f12 which use the file identifier for input or output of data.

None.

## 5  Arguments

1:     filenameconst char *Input
On entry: the name of the file to be opened. As special cases, an empty string or null pointer may be used to associate fileid with standard input or standard output. See the description of mode below.
Constraint: must contain a valid filename for the computer system being used.
2:     modeIntegerInput
On entry: specifies whether the file is to be opened for reading, writing or appending.
${\mathbf{mode}}=0$
The file is to be opened for reading.
If filename is an empty string or null pointer then fileid becomes associated with standard input.
${\mathbf{mode}}=1$
The file is to be opened for writing.
If filename is an empty string or null pointer then fileid becomes associated with standard output.
${\mathbf{mode}}=2$
The file is to be opened for appending.
If filename is an empty string or null pointer then fileid becomes associated with standard output.
Constraint: $0\le {\mathbf{mode}}\le 2$.
3:     fileidNag_FileID *Output
On exit: an identifier associated with the file to be read from, written to or appended to. This identifier will typically be passed to other NAG C Library functions.
4:     failNagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

## 6  Error Indicators and Warnings

NE_INT
On entry, ${\mathbf{mode}}=〈\mathit{\text{value}}〉$.
Constraint: $0\le {\mathbf{mode}}\le 2$.
NE_NOT_APPEND_FILE
Cannot open file $"〈\mathit{\text{value}}〉"$ for appending.
Cannot open file $"〈\mathit{\text{value}}〉"$ for reading .
NE_NOT_WRITE_FILE
Cannot open file $"〈\mathit{\text{value}}〉"$ for writing .

Not applicable.

None.

## 9  Example

This example illustrates how to open a file for writing, write a line to the file using nag_write_line (x04bac) and close the file using nag_close_file (x04adc). The file is then reopened, the line read back using nag_read_line (x04bbc), and closed again.

### 9.1  Program Text

Program Text (x04acce.c)

None.

### 9.3  Program Results

Program Results (x04acce.r)