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

后端 未结 3 1860
名媛妹妹
名媛妹妹 2020-12-21 15:25

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)
             


        
相关标签:
3条回答
  • 2020-12-21 15:52

    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.

    0 讨论(0)
  • 2020-12-21 16:01

    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.

    0 讨论(0)
  • 2020-12-21 16:06

    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).

    0 讨论(0)
提交回复
热议问题