x04 Chapter Contents
x04 Chapter Introduction
NAG Library Manual

# NAG Library Function Documentnag_example_file_io (x04aec)

## 1  Purpose

nag_example_file_io (x04aec) reads command-line arguments and returns either a file pointer or a file name depending on the argument flag specified in argv.

## 2  Specification

 #include #include
 FILE * nag_example_file_io (int argc, const char *argv[], const char *flag, char **fname)

## 3  Description

nag_example_file_io (x04aec) returns either a FILE pointer or the name of a file depending on the argument flag specified in argv. If the argument flag is either "-data" or "-results", a FILE pointer is returned for data input or output respectively. If the argument flag is "-options", "-nag_write" or "-nag_read", a char pointer is returned in fname to hold the name of an options file, a data output or a data input file.

None.

## 5  Arguments

1:     argcintInput
On entry: the number of command-line arguments.
2:     argv[argc]const char *Input
On entry: the argument vector.
3:     flagconst char *Input
On entry: indicates which file pointer or file name will be returned by the function. nag_example_file_io (x04aec) searches the command-line arguments contained in argv for flag. If it is found, in say ${\mathbf{argv}}\left[i-1\right]$, the function examines ${\mathbf{argv}}\left[i\right]$ and if it is a filename the function returns either a file pointer or the filename as follows:
${\mathbf{flag}}=\text{"-data"}$
If ${\mathbf{argv}}\left[i\right]$ is a filename it specifies the data file to be opened for reading and a FILE pointer to that file is returned. Otherwise a FILE pointer to stdin is returned.
${\mathbf{flag}}=\text{"-results"}$
If ${\mathbf{argv}}\left[i\right]$ is a filename it specifies the results file to be opened for writing and a FILE pointer to that file is returned. Otherwise a FILE pointer to stdout is returned.
${\mathbf{flag}}=\text{"-options"}$
If ${\mathbf{argv}}\left[i\right]$ is a filename it specifies a file containing optional arguments to be opened for reading and this filename is returned in fname. Otherwise a default filename comprising the stem of the program name with a file extension of .opt (e.g., e04ucce.opt) is returned in fname. Note that memory is allocated internally to fname using NAG_ALLOC. It can be freed using NAG_FREE.
${\mathbf{flag}}=\text{"-nag_write"}$
If ${\mathbf{argv}}\left[i\right]$ is a filename it specifies a file to be opened for library output and this filename is returned in fname. Otherwise a NULL is returned in fname.
${\mathbf{flag}}=\text{"-nag_read"}$
If ${\mathbf{argv}}\left[i\right]$ is a filename it specifies a file to be opened for reading library input and this filename is returned in fname. Otherwise a NULL is returned in fname.
Constraint: ${\mathbf{flag}}=\text{}$ "-data", "-results", "-options", "-nag_write" or "-nag_read".
4:     fnamechar **Input
On exit: if the flag specified is "-options", "-nag_write" or "-nag_read", fname contains the name of the file for reading or writing.

None.

Not applicable.

Not applicable.

None.

## 10  Example

This program illustrates the use of nag_example_file_io (x04aec) to check for input and output file names on the command line, before making a call to nag_1d_aitken_interp (e01aac).

### 10.1  Program Text

Program Text (x04aece.c)

None.

### 10.3  Program Results

Program Results (x04aece.r)