How do I write a newline character in a character variable in Fortran?

11,691

You can use the NEW_LINE intrinsic function to query the character used to represent newline in formatted stream output. This character typically does what you want when you send it to the console. It is probably easiest to write it out using a separate character data descriptor in your format specification.

Using a slash edit descriptor is actually a request to progress to the next record. For an internal file (writing to a character variable), this is the next array element. In your example code you are passing a scalar as the character variable (so just one record), hence an end of file condition occurs.

Share:
11,691

Related videos on Youtube

user3222801
Author by

user3222801

Updated on June 04, 2022

Comments

  • user3222801
    user3222801 almost 2 years

    I want to store newline character in my character buffer variable buff. So far my code goes like this:

           program TYPE_CHECK
    
    c newline storage in buffer 
           character(100), dimension(10)  :: buff
           integer, dimension(10) :: x
           integer :: i
    
           do i=1,10
           x(i) = i
           enddo
    
           do j=1,10
           write(buff(j), 1) x(j), x(j)
     1     format(' This is line ', I3, /,
          *       ' This is newline ', I3)
           enddo
    
           do j=1,10
           write(*, "(A100)") buff(j)
           enddo
    
           end program TYPE_CHECK
    

    This gives the following error:

    At line 13 of file myfoo6.F
    Fortran runtime error: End of file
    
  • JohnE
    JohnE over 5 years
    Would you mind adding some sample code to make this a little more obvious?
  • G. LC
    G. LC almost 5 years
    @JohnE Here is an example taken from link write(*,'(A)') 'This is record 1.'//NEW_LINE('A')//'This is record 2.'