Register to reply

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

by uart
Tags: controll, fortran, new line, print, write
Share this thread:
uart
#1
Sep15-08, 12:51 PM
Sci Advisor
P: 2,751
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 writen 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 seperate 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.
Phys.Org News Partner Science news on Phys.org
Fungus deadly to AIDS patients found to grow on trees
Canola genome sequence reveals evolutionary 'love triangle'
Scientists uncover clues to role of magnetism in iron-based superconductors
mgb_phys
#2
Sep15-08, 02:03 PM
Sci Advisor
HW Helper
P: 8,953
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.
uart
#3
Sep15-08, 08:48 PM
Sci Advisor
P: 2,751
Quote Quote by mgb_phys View Post
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 wont 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 cant 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 dont 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. :(

alphysicist
#4
Sep15-08, 09:45 PM
HW Helper
P: 2,249
Fortran write (or print). How to controll new line or not

Quote Quote by uart View Post
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 wont 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 cant 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 dont 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:

write (*,"(3f8.3)") a,b,c
and to have two writes on the same line you can use, for example:

write (*,"(3f8.3)",advance="no") a,b,c
write (*,"(3f8.3)") d,e,f
uart
#5
Sep16-08, 08:44 AM
Sci Advisor
P: 2,751
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.
alphysicist
#6
Sep16-08, 10:53 AM
HW Helper
P: 2,249
Quote Quote by uart View Post
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:

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:

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.)
mgb_phys
#7
Sep16-08, 10:59 AM
Sci Advisor
HW Helper
P: 8,953
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.
MareRobins
#8
Aug3-11, 02:08 PM
P: 1
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


Register to reply

Related Discussions
Stokes theorem equivalent for cross product line integral Calculus & Beyond Homework 4
Clearing a deleting... print job from spooler Computing & Technology 2
A proposition: create a Physics Forums e-print server.. Forum Feedback & Announcements 19
Write a model ? General Math 0
Line in vs Microphone in Windows Computing & Technology 4