read_characters(n) 1.0 "flibs"

Name

read_characters - Read a file character by character

Table Of Contents

Synopsis

Description

The read_characters module defines a derived type with accompanying routines to read a file character by character. It uses features of Fortran 2003 to define an object-oriented interface and is meant as a basic class for more sophisticated modules.

DATA TYPES AND ROUTINES

The module defines a single data type, CHARACTER_STREAM, and several functions and subroutines:

use read_characters

The name of the module

type(CHARACTER_STREAM)

The class defining the functionality

integer, parameter :: single_character, end_of_line, end_of_file, read_error

Parameters defining the type of result from the get method.

call stream%open( filename, error )

Open the file "filename" using an unused LU-number. If some error occurs, the argument "error" is set to true.

class(character_stream) stream

The variable by which to reference the file

character(len=*), intent(in) filename

The name of the file to open

logical, intent(out) error

Argument indicating whether opening the file was successful or not. Note that the file is opened with read/write access (though not explicitly) and that it is opened in such a way that the record length is 4 bytes. If this is not possible (for any number of reasons), an error is returned.

call stream%close( stream )

Close the file that was opened as a stream.

class(character_stream) stream

The variable by which to reference the file

call stream%get( char, type )

Get the next character from the file. The type argument indicates if it is an ordinary character, end-of-line or end-of-file.

class(character_stream) stream

The variable by which to reference the file

character(len=1) intent(out) char

The character that was read (if type is set to single_character.

integer, intent(out) type

Indicator, one of: single_character, end_of_line, end_of_file or read_error.