Fortran write (or print). How to controll new line or not

In summary: I thought that if I used the "advance='no'" specifier then it would force the next write statement to start on the same line as the previous one, instead of starting a new line. But as it turns out, that's not actually the case, it just uses the '*' specifier by default.In summary, you can use the '*' format specifier to write multiple variables on the same line, but you can also use the '(f8.3)' format specifier to write variables on separate lines.
  • #1
uart
Science Advisor
2,795
21
Fortran write (or print). How to controll "new line" or not

I haven't touched fortran for about 100 years (and that's only a very slight exaggeration). Recently I had to write a very small amount of code in Fortran 95. I've picked up most aspects pretty quickly but I'm still a bit vague on formating output (using either print or write).

One thing I can't figure out is how to control whether or not something is written on a new line or not. In the limited trial and error tests I've done it seems that (at least with numerical arguments) printing multiple variables in the one print statement puts them on the same line if I use the "generic" format string '*' and puts them on separate lines if use a specific format string, like "(f8.3)" for example.

I'm assuming that there is some way to control where and when print (or write) takes a new line. Can someone help me out.

thanks.
 
Technology news on Phys.org
  • #2


Back when I did fortran ( which involved the hull design on the Ark!) you put $ at the end of the format statement, I think in fortran90 there is "advance='no'" as well, not sure if the $ still works.
 
  • #3


mgb_phys said:
Back when I did fortran ( which involved the hull design on the Ark!) you put $ at the end of the format statement, I think in fortran90 there is "advance='no'" as well, not sure if the $ still works.

The Ark, hehe good one :D

Anyway that "advance=" specifier was what I was looking. The form of the write statement is,

"write (file_number,format_spec,advance='no') variable_name"

It's still a bit quirky, here's what I've found.

- If you use the default format specifier * then it won't let you use advance='no', it compiles but gets a run-time error "advance not allowed here".

- But if you use the defualt format * then it automatically writes multiple variables (in a single write staement) on the same line if possible. As in "write (*,*) a,b,c", variables a,b and c are all written on the same line. However since you can't use the advance specifier here (for some unknown reason) then you have no option about what the next write statement will do, it will always use a new line.

- But if you don't use the default format *, and instead specify a format string like "(f8.3)" for example, then it wont write multiple variables to the same line.
For example "write (*,"(f8.3)") a,b,c" will write variables a,b and c each to a new line.

- If you add the advance='no' specifier then it only effects whether of not the next write statemnt starts a new line.
So for example write (*,"(f8.3)",advance='no') a,b,c still writes a,b and c on different lines, but the next write statement will start it's output on the same line a "c" was written.

Dam this is quirky and horrible. :(
 
  • #4


uart said:
The Ark, hehe good one :D

Anyway that "advance=" specifier was what I was looking. The form of the write statement is,

"write (file_number,format_spec,advance='no') variable_name"

It's still a bit quirky, here's what I've found.

- If you use the default format specifier * then it won't let you use advance='no', it compiles but gets a run-time error "advance not allowed here".

- But if you use the defualt format * then it automatically writes multiple variables (in a single write staement) on the same line if possible. As in "write (*,*) a,b,c", variables a,b and c are all written on the same line. However since you can't use the advance specifier here (for some unknown reason) then you have no option about what the next write statement will do, it will always use a new line.

- But if you don't use the default format *, and instead specify a format string like "(f8.3)" for example, then it wont write multiple variables to the same line.
For example "write (*,"(f8.3)") a,b,c" will write variables a,b and c each to a new line.

Did you want them on the same line? As you have it written it's coming to the end of the specifier list while you still have values to output. For them all on the same line, you might try:

Code:
write (*,"(3f8.3)") a,b,c

and to have two writes on the same line you can use, for example:

Code:
write (*,"(3f8.3)",advance="no") a,b,c
write (*,"(3f8.3)") d,e,f
 
  • #5


As you have it written it's coming to the end of the specifier list while you still have values to output.

Yes alphysicist that fixed it, thanks.

I may have misread the documentation because I thought it said that if it got to the end of the format specifier and there were still items in the list then it just repeated from the beginning of the format specifier. Anyway that's what it seemed to be doing because it was printing each of the 3 variables as per the given format specifier, just not putting them on the same line. Anyway doing it properly as you've indicated with the count prefix works nicely. At last I feel in control of my output, yeah!

BTW. It's really interesting to look at Fortran after all these years. It's changed so much and adopted so many things from other languages, I got a bit of a surprise really. My recollections of Fortran were pretty bad, but Fortran95 (or later) actually looks like it would be quite a nice language for handling number crunching stuff. I especially love the inclusion of complex numbers as an intrinsic data type with all the mathematical functions natively handling complex argument. I also really like the MATLAB style of handling array ranges and sub-ranges.
 
Last edited:
  • #6


uart said:
Yes alphysicist that fixed it, thanks.

I may have misread the documentation because I thought it said that if it got to the end of the format specifier and there were still items in the list then it just repeated from the beginning of the format specifier. Anyway that's what it seemed to be doing because it was printing each of the 3 variables as per the given format specifier, just not putting them on the same line. Anyway doing it properly as you've indicated with the count prefix works nicely. At last I feel in control of my output, yeah!

You're right about it repeating from the beginning (though how it repeats is a bit more complicated if your format specifier list has nested parenthesis). However, fortran assumes that when you come to the end of a specifier list that you want to start a new line. For example, the line I posted could be written as either of these two equivalent lines:

Code:
write (*,"(3f8.3)") a,b,c
write (*,"(f8.3,f8.3,f8.3)") a,b,c

but if you compare that to the ouput of either of these two equivalent lines:

Code:
write (*,"(2f8.3)") a,b,c
write (*,"(f8.3,f8.3)") a,b,c

you can see how it handles the repeating.



BTW. It's really interesting to look at Fortran after all these years. It's changed so much and adopted so many things from other languages, I got a bit of a surprise really. My recollections of Fortran were pretty bad, but Fortran95 (or later) actually looks like it would be quite a nice language for handling number crunching stuff. I especially love the inclusion of complex numbers as an intrinsic data type with all the mathematical functions natively handling complex argument. I also really like the MATLAB style of handling array ranges and sub-ranges.

I definitely think it's great for calculations--especially how it handles arrays, which is a marked improvement in fortran95. (My Fortran experience has been the opposite of yours--I started on fortran 77, and since then have used the language steadily. When fortran 90 and then fortran 95 came out, there was not a sudden transition for me. I just kept writing mainly fortran 77 style programs (except in free form) and slowly over time I used more and more of the new features until they all seemed natural.)
 
  • #7


I haven't used it seriously since the F77 days but it does have afew advantages over c++. Because it can guarantee some pointer operation isn't going to change an array the compiler can automatically parallize a lot of code which is going to start being important for the multicore CPUs we are being promised - it might start to make a come back.
 
  • #8


Hi all,

I am writing to text files (with Fortran 77), and I would prefer to use just * for format so I don't have to get specific with every column in every file.
I thought that I understood that if you:
write(12,*) a(i), b(i), c(i)
then each time it hits the write statement it will create a new line, but during a given print statement it will not advance a line on its own.

I have some very long lines of data (10-15 columns of data) and it seems to be automatically advancing a line in the middle of that data.

Do you have any suggestions to maintain the auto format (or not have to set it to rounding decimal places with F) but keep all columns in a single write statement on one line?

Many thanks!
Mare
 

1. How do I use the "write" or "print" command in Fortran?

The "write" and "print" commands in Fortran are used to display output on the screen or write it to a file. The syntax for these commands is:
WRITE (*, *) variable
or
PRINT *, variable
Where variable is the value or variable you want to display. The asterisks (*) represent the output format and can be replaced with specific format codes such as "I" for integers or "F" for floating point numbers.

2. How can I control when a new line is created when using "write" or "print" in Fortran?

To control when a new line is created, you can use the "advance" statement after the format code in the "write" or "print" command. For example, if you want to print two values on the same line, you can use:
WRITE (*, '(I2, F5.2)', ADVANCE="NO") integer_variable, float_variable
This will print the integer value followed by the float value without creating a new line. You can also use "ADVANCE='YES'" to reset the output to the beginning of the next line.

3. Can I print multiple lines of output using a single "write" or "print" command?

Yes, you can use the "advance" statement to print multiple lines of output using a single "write" or "print" command. For example, if you want to print three values on separate lines, you can use:
WRITE (*, '(I2)') integer_variable
WRITE (*, '(F5.2)') float_variable
WRITE (*, '(A10)') string_variable
This will print each value on a separate line without the need for multiple "write" or "print" commands.

4. How do I include special characters or formatting in my output using "write" or "print" in Fortran?

To include special characters or formatting in your output, you can use format codes in the "write" or "print" command. For example, if you want to print a dollar sign before a monetary value, you can use the "$" format code:
PRINT *, '$', float_variable
This will print the dollar sign followed by the value of the float variable. You can also use other format codes such as "A" for character strings, "L" for logical values, and "E" for scientific notation.

5. Can I write or print to a specific file in Fortran?

Yes, you can use the "open" statement to specify a file for output in Fortran. The syntax for this statement is:
OPEN (UNIT=file_number, FILE='file_name', STATUS='NEW')
After opening the file, you can use the "write" or "print" command to write output to the specified file. To close the file, use the "close" statement:
CLOSE (UNIT=file_number)
It is important to note that the "file_number" must be a unique integer value and the "file_name" must be in quotes.

Similar threads

  • Programming and Computer Science
Replies
14
Views
3K
  • Programming and Computer Science
Replies
16
Views
2K
  • Programming and Computer Science
Replies
22
Views
4K
  • Programming and Computer Science
Replies
19
Views
5K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
9
Views
7K
  • Programming and Computer Science
Replies
12
Views
7K
Replies
11
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
Back
Top