Nvfortran trim not trim newline

The nvfortran trim function not trim newline, but the Gfortran and ifort dose.
It makes me have lot works to do. when I migrating a old fortran program to cuda fortran.

Is this behavior a bug OR expect?

for example:

    write(*,*) 'should be 9 but get 10:',len(trim('nvfortran\n'))

    write(*,*) 'should be 9 and get 9:',len(trim('nvfortran '))

Hi jubilee2,

“TRIM” only removes trailing blank spaces, I don’t believe it’s expected to remove special characters like newlines.

In running your example, both gfortran an ifort don’t treat “\n” as a newline character, but even when using a string with a newline appended, they still don’t remove it.

Is the string you’re using coming from C? Can you provide an example which illustrates the issue in your full program?

Example:

$ cat test.f90
    write(*,*) 'should be 9 but get 10:',len(trim('nvfortran\n')), trim('nvfortran\n')
    write(*,*) 'should be 9 and get 9:',len(trim('nvfortran ')), trim('nvfortran ')
    end
$ gfortran test.f90; a.out
 should be 9 but get 10:          11 nvfortran\n
 should be 9 and get 9:           9 nvfortran
$ ifort test.f90; a.out
 should be 9 but get 10:          11 nvfortran\n
 should be 9 and get 9:           9 nvfortran
$ nvfortran test.f90; a.out
 should be 9 but get 10:           10 nvfortran

 should be 9 and get 9:            9 nvfortran
$ cat test2.f90

    character(80) :: str1 = "nvfortran"//char(10)
    character(80) :: str2 = "nvfortran "
    write(*,*) 'should be 9 but get 10:',len(trim(str1)), trim(str1)
    write(*,*) 'should be 9 and get 9:',len(trim(str2)), trim(str2)
    end

$ gfortran test2.f90; a.out
 should be 9 but get 10:          10 nvfortran

 should be 9 and get 9:           9 nvfortran
$ ifort test2.f90; a.out
 should be 9 but get 10:          10 nvfortran

 should be 9 and get 9:           9 nvfortran
$ nvfortran test2.f90; a.out
 should be 9 but get 10:           10 nvfortran

 should be 9 and get 9:            9 nvfortran
1 Like

I should mention, if you want nvfortran to match the behavior of gfortran/ifort in the first example, i.e. treat “\n” as two characters instead of a newline, add the flag “-Mbackslash”.

$ nvfortran -Mbackslash test.f90; a.out
 should be 9 but get 10:           11 nvfortran\n
 should be 9 and get 9:            9 nvfortran
1 Like

Thanks for quick responding. Yes I test again. THEN realize those newline char comes from read() function.

I first think the issue related with “trim” because the error messages say that line have error during passing the string into a dynamic library (mylib.so) function.

thanks for help.

call someting_c_fuction(filename//C_NULL_CHAR)

by the way if I find newline issue in read function do you want me replay in this chain or open other post?

HI every one,

now I realized it is CR LF things. The Nvfortran read function only remove “LF” char but keep “CR” char

program test
  character(len=20) :: readLine
  open(11,file='txt.txt')
  read(11,*) readLine
  close(11)
  print *, len(trim(readLine))
end

txt.txt (under CR LX mode)(windows mode)

hello

result

$ nvfortran test.f90 && ./a.out
            6
$ gfortran test.f90 && ./a.out
           5
$ 

Two solutions.

  1. Run the “dos2unix” utility on your txt file to convert it to use UNIX formatting, or
  2. Set the environment variable FORTRANOPT set to “crlf” to have our runtime use Windows style formating.

Example:

% nvfortran test.f90; a.out
            6 hello
% setenv FORTRANOPT crlf
% nvfortran test.f90 ; a.out
            5 hello

Hope this helps,
Mat

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.