Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 859
user3222801 Avatar asked Jan 22 '14 09:01

user3222801


People also ask

How do I add a new line in Fortran?

NEW_LINE(C) returns the new-line character. The argument shall be a scalar or array of the type CHARACTER . Return value: Returns a CHARACTER scalar of length one with the new-line character of the same kind as parameter C .

How do I write a string in Fortran?

The 'string' of characters is achieved by including the ('LEN') length specifier to the declaration statement. You should also be aware that when dealing with 'CHARACTER' types the single quote character (')or the double quote character (") can be used as the delimiters for the character type.

How do I continue a line in Fortran 77?

Continuation. Any character can be used instead of the plus sign as a continuation character. It is considered good programming style to use either the plus sign, an ampersand, or digits (using 2 for the second line, 3 for the third, and so on).


1 Answers

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.

like image 108
IanH Avatar answered Oct 14 '22 22:10

IanH