関連情報
ホーム > FortranでCSV形式ファイルを扱う

FortranでCSV形式のファイルを読み書きするモジュール

Fortranでは比較的簡単にCSVファイルへ出力したり、あるいはCSVファイルから読み込みが行えます。

参考:
  FortranでCSVファイルを読み込む方法
  FortranでCSVファイルへ書き出しを行う方法

しかしながらシンプルなCSVファイルではない、より複雑な事を行いたい場合には工夫が必要となります。 ここではシンプルなCSVの読み書きでは対応できないような場合に利用可能なモジュールを以下にご紹介します。

CSV形式の処理を行うモジュールを公開

FortranでCSV(Comma-Separated-Value)形式(Excel等で入出力が可能な形式)のファイルをFortranプログラムで読んだり書いたりするためのモジュールをご紹介します。 Fortran 2003の機能であるtype-bound procedureの機能が使われていますので、type-bound procedureの機能が実装済みのコンパイラでご利用いただくことが可能です。※下記のモジュール及びサンプルはNAG Fortran Builderを用いて稼働確認をいたしております。

CSV形式の読み書きを行うモジュールのダウンロード

csv_fileモジュール(csv.f90

csv_fileモジュールを利用するサンプル(example.f90

csv_fileモジュール詳細説明(英語)

1 Introduction

This document describes the module “csvfile_module”, which can be used for reading Comma-Separated-Value files.

2 Types

csvfile_t

This is a derived type representing the state of a CSV file. All of its components are private, so it can only be manipulated using its type-bound procedures.

open_action_t

This is a private type; only the three named constants of the type are available for use as the action argument to the open procedure.

open_status_t

This is a private type; only the four named constants of the type are available for use as the status argument to the open procedure.

3 Variables

Integer :: csv_unit_range_bottom = 10000

This variable stores the lowest unit number that will be used to open a CSV file.

Integer :: csv_unit_range_top = 10500

This variable stores the highest unit number that will be used to open a CSV file.

4 Constants

Type(open_action_t),Parameter :: csvopen_read

Named constant for specifying ACTION='READ' in the CSV file open.

Type(open_action_t),Parameter :: csvopen_readwrite

Named constant for specifying ACTION='READWRITE' in the CSV file open.

Type(open_action_t),Parameter :: csvopen_write

Named constant for specifying ACTION='WRITE' in the CSV file open.

Type(open_status_t),Parameter :: csvopen_old

Named constant for specifying STATUS='OLD' in the CSV file open.

Type(open_status_t),Parameter :: csvopen_new

Named constant for specifying STATUS='NEW' in the CSV file open.

Type(open_status_t),Parameter :: csvopen_replace

Named constant for specifying STATUS='REPLACE' in the CSV file open.

Type(open_status_t),Parameter :: csvopen_unknown

Named constant for specifying STATUS='UNKNOWN' in the CSV file open.

5 Type-bound Procedures

In the following descriptions, the functions and procedures are described using this%procname; here, this is a Class(csvfile_t) variable and procname is the type-bound procedure name.

Nearly all procedures have iostat and iomsg arguments, and many have eor and eof arguments; these are described in the next section and the descriptions will be omitted from the individual procedures.

 
Logical Function this%at_eof(iostat,iomsg)

returns .True. if and only if the CSV file is positioned at the end.

 
Logical Function this%at_eor(iostat,iomsg)

returns .True. if and only if the CSV file is positioned at the end of the current record (i.e. all the values in the current record have already been read).

 
Subroutine this%close(iostat,iomsg)

Closes a CSV file.

 
Subroutine this%open(file,action,status,decimal,iostat,iomsg)
    Character(*),Intent(In) :: file
    Type(open_action_t),Intent(In),Optional :: action
    Type(open_status_t),Intent(In),Optional :: status
    Character,Intent(In),Optional :: decimal

Opens a CSV file with the given name (file). The default action is csvopen_read, the default status is csvopen_unknown and the default decimal is '.'. Note that only '.' and ',' are valid values for decimal; an error will be raised on any other value.

 
Subroutine this%read(value,iostat,iomsg,eor,eof)
  Integer(Int8),Intent(Out) :: value
  Integer(Int16),Intent(Out) :: value
  Integer(Int32),Intent(Out) :: value
  Integer(Int64),Intent(Out) :: value
  Real(Real32),Intent(Out) :: value
  Real(Real64),Intent(Out) :: value
Read a value from the file into value, which may be any of the types listed. An error will occur if the form of the file data is incorrect for the type.

 
Subroutine this%read(value,length,iostat,iomsg,eor,eof)
    Character(:),Allocatable :: value
    Integer,Intent(Out),Optional :: length
Read a value as a character string from the file into value. If length is not present, value will be allocated to be the exact length of the input value. If length is present, value will be allocated to be at least as long as the value in the file, but might be longer; length will contain the actual length of the value in the file.

6 Error, End-of-file and End-of-record conditions

An end-of-record condition occurs if an attempt is made to read a value past the end of the record. An end-of-file condition occurs if an attempt is made to read a value or to advance to a record past the end of file. An error condition occurs on any error, including the csvfile_t variable not being connected to a file, or the value in the file not having the right format.

The arguments used to handle error, end-of-file and end-of-record conditions are:

 
  Integer,Intent(Out),Optional :: iostat
  Character(*),Intent(InOut),Optional :: iomsg
  Logical,Intent(Out),Optional :: eor,eof

If iostat is present, it is assigned zero for success, the negative value Iostat_Eor for end-of-record, the negative value Iostat_eof for end-of-file, and a positive value for an error condition.

If iomsg is present and any condition occurs, it is assigned an explanatory message. If it is shorter than the message, the message will be truncated.

If eor is present, it is set to .True. if an end-of-record condition occurs and to .False. otherwise.

If eof is present, it is set to .True. if an end-of-file condition occurs and to .False. otherwise.

If an error condition occurs and iostat is not present, or an end-of-record condition occurs and both iostat and eor are not present, or an end-of-file condition occurs and both iostat and eof is not present, the program is terminated with an explanatory message.

Results matter. Trust NAG.
Privacy Policy | Trademarks