WillowFinch
WillowFinch

Reputation: 119

format specifiers with no newline and multiple writes in fortran

I want to write a header for an output file that's aligned with several variables, something like:

var1               var2 ................. varX
value              value                  value

So I'd rather avoid manually padding every variable name. From reading the docs, a format specifier when calling write() is the preferred method.

write(1, "(a23)") "var1", "var2", !etc.

But this inserts a newline after every variable. write(1, "(a23)", advance="no") does NOT fix this, it only suppresses the newline after the final variable is written. write(1, *) DOES print a newline only after the final variable, but then I lose the spacing.

Tested in GNU Fortran 10.2 with -std=f2003.

Ideas?

Upvotes: 1

Views: 339

Answers (1)

"(a23)" is the format for a single character variable. When the format finishes, but there are still items to be written in the input list, the format is interpreted from the beginning, but a new line character (end of record) is added.

You have to use a format for multiple items: "(3a23)", "(3(a23))", "(9999(a23))" or even "(*(a23))"

See also https://stackoverflow.com/a/9881337/721644

Note that you can also use the t or tr or x descriptor to insert padding.

Upvotes: 1

Related Questions