user3222801
user3222801

Reputation: 61

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

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

Upvotes: 6

Views: 12911

Answers (1)

IanH
IanH

Reputation: 21431

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.

Upvotes: 10

Related Questions