|
|
|
Formated I/O
Overview
Formatted i/o is supported in the ObjexxFCL by a powerful system that integrates Fortran format specifications into a C++ stream context. High-level components provide read, write, inquire, rewind, and backspace support. Fortran-style status/error handling is provided by an optional flags object in these calls. The result is a concise usage that is natural for both Fortran and C++ users.
A lower-level formatted i/o system is also available for use when the full power of Fortran format control is not needed. Alternative systems such as Boost.Format can also be used alongside ObjexxFCL formatted i/o.
Read
Formatted read operations use a Read call with a stream or string (for "internal" reads) argument and a format argument. Here are some samples:
Read( a_stream, "(F10.0,I5,F10.0)" ) >> x >> i >> M(j);
Read( a_string, "(3F15.0)" ) >> a >> b >> c; // Internal read
Read( a_stream, "(3F15.0)", flags ) >> a >> b >> c; // IOFlags argument
Read( a_stream, "*" ) >> name >> age >> phone; // List-directed
|
The i/o support is quite complete:
- Fortran Format support is nearly Fortran 2008 complete and all typical descriptors and usage is supported, including repeat counts, reversion logic, complex variables, and so forth.
- File and string streams are supported, including std::cin and std::cout.
- The IOFlags argument can control the read behavior (such as equivalents to the BLANK and ADVANCE Fortran READ arguments) and returns the i/o status and error/end state for local processing. If no IOFlags argument is used with handling set an i/o error will cause an error message and program exit, as in Fortran.
Using the i/o flags is straightforward and familiar to Fortran developers, as in this example:
IOFlags flags;
flags.na().ios_x(); // Turn on non-advancing mode and status handling
Read( a_stream, "(3F15.0)", flags ) >> a >> b >> c;
if ( flags.end() ) goto Label99; // Can also use flags.ios() value
if ( flags.err() ) goto Label42;
|
Write
Similar to Read, formatted write operations use a Write call with a stream or string (for "internal" writes) argument and a format argument. Here are some samples:
Write( a_stream, "(F10.0,I5,F10.0)" ) << x << i << M(j);
Write( a_string, "(3F15.0)" ) << a << b << c; // Internal read
Write( a_stream, "(3F15.0)", flags ) << a << b << c; // IOFlags argument
Write( a_stream, "*" ) << name << age << phone; // List-directed
|
Print
Print is just a formatted write to the console. No stream argument is needed and, like Fortran's PRINT, no status/control is available so no IOFlags argument is used. Here are some samples:
Print( "(3(I5,2X))" ) << i << j << k;
Print( "*" ) << name << age << phone; // List-directed
|
Low-Level Formatted Input
The low-level formatted input capability allows formatted reads to be performed using C++ streams, as in:
int i, j; stream >> bite(3,i) >> skip(1) >> bite(2,j) >> skip;
|
A numeric first argument to bite is the number of characters to read the variable into. Variables of a known width, such as char, can be read with a one-argument bite function, as in bite(name). Character strings are read into the variables as is: no BLANK=ZERO or nP Fortran-like read capability is available at this time.
A numeric argument to skip is the number of characters to skip over. A trailing skip reads the rest of the line and the line terminator.
Low-Level Formatted Output
The low-level formatted output capability provides the per-item Fortran formatting with C++ streams, as in:
int i, j; double x, y;
std::string name;
std::string address;
...
stream << I(i,3) << space(1) << I(2,j) << '\n';
stream << F(x,9,3) << G(y,15,6) << '\n';
stream << A(name,15) << '\n';
stream << A(address,30) << '\n';
|
The floating point output functions have the form F(v,w,d), E(v,w,d,e), and G(v,w,d,e) where v stands for the variable, w is the field width, d is the number of fractional digits, and e is the number of exponent digits.
Formatting similar to Fortran list-directed writes are provided by some additional functions. List-directed write formatting is not standardized. The LD functions provide list-directed write formatting with each field's width and precision based on its data type, provided by the TraitsLD templates. The TraitsLD matches Intel Fortran behavior: alternatives could be developed to match the behavior of other compilers.
Organization
The Read, Write, Print, Inquire, Rewind, and Backspace services are in separate files with those names. So, for example, to use Read the Read.hh header should be included.
Format objects are handled internally by the i/o operations so application code rarely needs to work with it explicitly, but it can be found in the Format.hh and Format.cc files.
The low-level formatted i/o support is contained within the nested ObjexxFCL::fmt namespace to provide control over scoping and name conflicts. The fmt.hh header should be included to use this support.
|
|
|
|