Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Problem with TRIM and getcwd

Felix_F_1
Einsteiger
1.985Aufrufe

Hi everyone,

The TRIM function in combination with getcwd doesn't do anything for me. Why is that so? Any ideas?

CHARACTER(len=255)                           :: cwd
Character(:),allocatable                            :: trim_cwd


CALL getcwd(cwd)
trim_cwd = TRIM(cwd) ! still holds all the trailing spaces

 

Thanks,
Felix 

0 Kudos
9 Antworten
Steven_L_Intel1
Mitarbeiter
1.985Aufrufe

Your program doesn't demonstrate anything - how do you know TRIM doesn't work? When I construct an example using your code, it does work for me.

    program U506416

   CHARACTER(len=255)                           :: cwd
Character(:),allocatable                            :: trim_cwd


CALL getcwd(cwd)
trim_cwd = TRIM(cwd) ! still holds all the trailing spaces
print *, len(trim_cwd), trim_cwd

    end program U506416
          19 C:\Projects\U506416

 

Felix_F_1
Einsteiger
1.985Aufrufe

I am aware that it should work, since I make use of TRIM frequently in my code.
However in this particular situation a path with character length of 62 to the working directory is not trimmed. The length remains 255. Also len_trim() returns 255 as length.

So there is no known issue I have to consider here?
Is there any workaround?

Thanks,
Felix

 

 

DavidWhite
Geschätzter Beitragender II
1.985Aufrufe

What is Len_Trim(cwd)?

David

DavidWhite
Geschätzter Beitragender II
1.985Aufrufe

Like Steve, I find that your program works as expected with exactly just the directory name in trim_cwd.

I suspect that the code you have given us here is not the exact code in your program, and that something else is happening to cwd before you do the assignment to trim_cwd.  Note that cwd will in fact still have the full string, including all trailing blanks.

Regards,

David

Xj_Kong
Anfänger
1.985Aufrufe

Actually, I agree that this happened before for me as well. Unfortunately, I forgot how to change the project setting to correct that. 

Anyway, I avoid to use trim() and len_trim() by ctrim() and clen().

PURE INTEGER FUNCTION Clen(s,endl)    ! get len of string (C or tab\blank terminated)
CHARACTER(*),INTENT(IN)::s
character,INTENT(IN),optional::endl
character::endch
integer::nl,ne
endch=char(0); nl=len(s); 
if(present(endl))then; endch=endl; do while(s(nl:nl)==endch); nl=nl-1; if(nl==0)exit; enddo; endif
Clen=scan(s(:nl),endch,.false.)-1
IF(Clen==-1)then
    Clen=LEN_TRIM(s)  ! not C string, Ctrim==TRIM
else
    Clen=LEN_TRIM(s(:Clen))  ! if C string, Ctrim==remove blank before NULL    
end if
END FUNCTION Clen


pure FUNCTION Ctrim(s1)  RESULT(s2)   ! trim a string (C or blank terminated)
CHARACTER(*),INTENT(IN)::s1 !+min(1,scan(s1,char(0)))
character(clen(s1//"")+min(1,scan(s1,char(0))))::s2 ! //"" to convert pointer string to character string and set output length to 1st null -1 or last char
s2=s1; if(scan(s1,char(0))/=0)s2=s2(:clen(s2))//""C
END FUNCTION Ctrim

 

Steven_L_Intel1
Mitarbeiter
1.985Aufrufe

There is no project setting relevant to this.

jimdempseyatthecove
Geehrter Beitragender III
1.985Aufrufe

Felix F. wrote:

I am aware that it should work, since I make use of TRIM frequently in my code.
However in this particular situation a path with character length of 62 to the working directory is not trimmed. The length remains 255. Also len_trim() returns 255 as length.

So there is no known issue I have to consider here?

Is it possible that your original string was NULL terminated after a series of trailing blanks?

In this case, as far as Fortran is concerned, the NULL character is a significant non-blank character.

Jim Dempsey

rase
Neuer Beitragender I
1.985Aufrufe

I had a similar problem when I was a newbee at mixed programming. I was not aware that the terminating Null character in character strings transfered to C programs by a calling Fortran program is not the same as a blank character. I would check the string for nonprintable characters, including the Null character. Nonprintable characters could have been inserted for other reasons than mixed programming.

Steven_L_Intel1
Mitarbeiter
1.985Aufrufe

I see in another of Felix's posts that he is apparently using version 11.0. In that version, one did need to use /assume:realloc_lhs for allocatable, deferred-length character variables. That requirement was removed in version 11.1 in 2009.

Antworten