Programmer Guide > Format Strings > Using Format Reversion
  

Using Format Reversion
If the data is all of the same type, you can abbreviate the C and FORTRAN format strings using the technique of format reversion. Format reversion is a shorthand way of specifying a format string.
For more information on format reversion with FORTRAN format strings, refer to a FORTRAN 77 handbook.
Example—Using Format Reversion to Write Integer Data
This example writes data to a file using a single C format string:
var1 = INDGEN(20)
status = DC_WRITE_FIXED("simple.dat", var1, Format="5%i")
Similarly, the entire file can be written with other PV-WAVE statements using the following FORTRAN format string:
OPENW, 1, "simple.dat"
var1 = INDGEN(20) +1
PRINTF, 1, var1, Format="(5(I4))"
CLOSE, 1
The abbreviated format strings repeatedly writes the integer values in var1 until all of the data has been transferred. The result is a data file, simple.dat, that contains 20 integer values:
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Example—Using Format Reversion to Read Floating-Point Data
The following data file, tesla.dat, contains only floating point numbers:
.8945  .5768  .3958  .3098  .8948  .8495
.0938  .8749  .4798  .9204  .2479  .9485
This entire file can be read using a single C format string:
status = DC_READ_FIXED('tesla.dat', var1, Format="%f")
This abbreviated format string repeatedly reads or writes floating point numbers until all of the data are read or written.
Similarly, the entire file can be read with other PV-WAVE statements using the following FORTRAN format string:
OPENR, 1, "tesla.dat"
var1 = FLTARR(12)
READF, 1, var1, Format="(F5.4,2X)"
CLOSE, 1
This FORTRAN format string example assumes that there are two spaces between each value, as represented by the 2X.

Version 2017.0
Copyright © 2017, Rogue Wave Software, Inc. All Rights Reserved.