alb_j
alb_j

Reputation: 85

gfortran error: unexpected element '\' in format string at (1)

I have a project written in VS2010 with Intel Visual Fortran. I have a dump subroutine to write a 2D matrix into file:

subroutine Dump2D(Name,Nx,Ny,Res)
    implicit none
    integer i,j,Nx,Ny
    real(8) :: Res(Nx,Ny)
    character(len=30) name,Filename
    logical alive
    write(filename,*) trim(Name),".dat"
    Write(*,*) "Saving ",trim(Name)," Please wait..."
    open (10,file=filename)
    do i=1,Ny
           Write(10,FMt="(D21.13\)")   (Res(j,i),j=1,Nx)
           Write(10,*)  
    end do
    close(10)
    Write(*,*) "Save ",trim(Name),"Complete!"  
    return
end subroutine Dump2D

It is ok to compile and run. But when I compile in emacs using gfortran it gives me the error:

I think it's because the gfortran doesn't recognize \ in a format for a write command. How do I fix this problem?

                Write(10,FMt="(D21.13\)") (Res(j,i),j=1,Nx)
                                   1
Error: Unexpected element '\' in format string at (1)

Upvotes: 2

Views: 3528

Answers (3)

The backslash is not valid in Fortran 77 FORMAT statements. Gfortran will not compile it, unless you fix the code. There is no flag that will change that AFAIK (-fbackslash should not help here).

If I understand the intention correctly (and I may be wrong), the backslash does the same as the dollar sign in some other compilers and prevents terminating a record (line). In that case the advance="no" put in the write statement should help. It is Fortran 90, but you should not avoid it just for that reason.

Upvotes: 1

SteveES
SteveES

Reputation: 554

Another approach (although francescalus answer is better in your case) would be to build a format string that contains the number of elements to include in your row. One way of doing this is to use the following to build the format string (which uses an explicit space character to separate elements within a line in the file):

WRITE(fmtString, '(A,I0,A)') '(', Nx, '(D21.13,:,1X))'    *

Then use the format string variable in your WRITE statement as so:

do i=1,Ny
   Write(10,FMt=fmtString)   (Res(j,i),j=1,Nx)
end do

This approach can also be very useful if you want to use something other than spaces to separate elements (e.g. commas or semicolons).

*As that's a little difficult to read, I will provide an example. For Nx = 3, this would be equivalent to:

fmtString = '(3(D21.13,:,1X))'

Which is 2 numbers formatted using D21.13, each followed by a space, and a final number formatted using D21.13, but without a space after it (as the ":" stops at the final item).

Upvotes: 2

francescalus
francescalus

Reputation: 32366

The edit descriptor \ relates to backslash editing. This is a non-standard extension provided by the Intel compiler (and perhaps others). It is not supported by gfortran.

Such backslash editing is intended to affect carriage control. Much as in this answer such an effect can be handled with the (standard) non-advancing output.1

As you simply want to output each column of a matrix to a record/line you needn't bother with this effort.2 Instead (as you'll see in many other questions):

do i=1,Ny
   write(10,fmt="(*(D21.13))") Res(:,i)
end do

There are also other approaches which a more general search will find.


1 The Intel compiler treats \ and $ in the same way.

2 There are subtle aspects of \, but I'll assume you don't care about those.

Upvotes: 3

Related Questions