Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to break to new line in Fortran when printing?

It is very simple query but I'm not able to find the exact solution. How to break to new line when printing in Fortran?

for example

print*,'This is first line'
print*,'This is second line'

I want following output

This is first line

This is Second line

That is add space between two lines.

In java we use \n and in html using <br> does the job..but how to achieve same in Fortran?

like image 395
Pro Academy Avatar asked Sep 28 '19 05:09

Pro Academy


People also ask

How do I continue a statement in Fortran?

In Fortran, a statement must start on a new line. If a statement is too long to fit on a line, it can be continued with the following methods: If a line is ended with an ampersand, &, it will be continued on the next line. Continuation is normally to the first character of the next non-comment line.

How do you end a line in Fortran with a&?

Fortran Continuation Lines If a line is ended with an ampersand, &, it will be continued on the next line. Continuation is normally to the first character of the next non-comment line. A = 174.5 * Year & + Count / 100

How to disable linebreak in Intel Fortran?

Intel provides the option to disable the automatic linebreak. Refer to "Intel® Fortran Compiler 17.0 Developer Guide and Reference", you will find the option Add it to compiler flags. Continue your normal work flow. And find some time to update your code later.

How do I print two lines of output?

There are several ways to print two lines of output. is one way to achieve what you want. Another is to do Show activity on this post. There are a number of ways to manage what you want. We can either print blank records or explicitly add a newline character. NEW_LINE ('a') is likely to have an effect like ACHAR (10) or CHAR (10,KIND ('a')).


2 Answers

There are several ways to print two lines of output.

program foo
   print *, 'This is the first line'
   print *, 'This is the second line'
end program

is one way to achieve what you want. Another is to do

program foo
   write(*,'(A,/,A)') 'This is the first line', 'This is the second line'
end program foo

And, yet another way

program foo
   write(*,'(A)') 'A' // achar(13) // achar(10) // 'B'
end program foo

And with some compilers you can use options

program foo
   write(*,'(A)') 'A\r\nB'
end program foo

Compiling with the following options yields:

$ gfortran -o z -fbackslash a.f90 && ./z
  A
  B
like image 127
evets Avatar answered Oct 16 '22 23:10

evets


There are a number of ways to manage what you want. We can either print blank records or explicitly add a newline character.

A newline character is returned by the intrinsic function NEW_LINE:

print '(2A)', 'First line', NEW_LINE('a')
print '(A)', 'Second line'

NEW_LINE('a') is likely to have an effect like ACHAR(10) or CHAR(10,KIND('a')).

A blank record can be printed by having no output item:

print '(A)', 'First line'
print '(A)'
print '(A)', 'Second line'

Or we can use slash editing:

print '(A,/)', 'First line'
print '(A)', 'Second line'

If we aren't using multiple print statements we can even combine the writing using these same ideas. Such as:

print '(A,:/)', 'First line', 'Second line'
print '(*(A))', 'First line', NEW_LINE('a'), NEW_LINE('a'), 'Second line'

NEW_LINE('a') could also be used in the format string but this doesn't seem to add much value beyond slash editing.

like image 22
francescalus Avatar answered Oct 17 '22 00:10

francescalus