# flibs/strings(n) 1.0 "flibs"

## NAME

flibs/strings - Writing CSV files

## TABLE OF CONTENTS

TABLE OF CONTENTS
SYNOPSIS
DESCRIPTION
ROUTINES
COPYRIGHT

## SYNOPSIS

use csv_file
call csv_next_record( lun )
call csv_write( lun, data )

## DESCRIPTION

The csv_file module facilitates the writing of CSV files. Whereas it is very easy to read CSV files with Fortran, using list-directed read statements:

        integer           :: i1, i2
        character(len=20) :: string
        real              :: f3
        read(10,*) i1, i2, string, f3

for instance, writing them is slightly more complicated. The module helps with this by two routines, csv_next_record and csv_write.

The module supports writing scalar variables, one- and two-dimensional arrays according to a simple scheme. As CSV files are ordinary formatted files where each field is separated by commas and strings are possibly delimited by quotation marks ("), the procedure is this:

- Open the file as a formatted file
- Pass the LU-number for the file to the routines mentioned above, using the second to write the data and the first to force a new record.

In more detail, the layout of the CSV-file can be described as follows:

- Single items are written to the end of the current record
- One-dimensional items are also written to the end of the current record
- Two-dimensional items are written to separate records, one for each row
- Except for the two-dimensional versions, all routines allow you to suppress advancing to the next record:
  - for single items you must indicate whether to advance or not
  - for one-dimensional items, the argument is optional. Default is to advance.

Note on the format: CSV-files apparently come in different guises (Kernighan and Pike, The practice of Programming, Addison-Wesley, 1999). This module uses the following rules:

- items are always separated by a single comma (,)
- string items are delimited by double quotes (")
- embedded double quotes are treated by doubling the quote
- trailing blanks are considered irrelevant

## ROUTINES

The module contains two public subroutines:

use csv_file
  To import the subroutines for writing a CSV file, use this module.
call csv_next_record( lun )
  Writes a new line to the file.
  integer lun
    The LU-number the file is connected to
call csv_write( lun, data )
  Writes data to the file in the proper format.
  integer lun
    The LU-number the file is connected to
  (...) data
    The data to be written to the file. The type can be:
    - an integer or a real (single or double precision) number or a character string
    - an integer or a real (single or double precision)one-dimensional array or a one-dimensional array of character strings
    - an integer or a real (single or double precision)two-dimensional array or a two-dimensional array of character strings

## COPYRIGHT

Copyright © 2005 Arjen Markus <arjenmarkus@sourceforge.net>