Problem with TRIM and getcwd

Problem with TRIM and getcwd

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 

10 帖子 / 0 全新
最新文章
如需更全面地了解编译器优化,请参阅优化注意事项

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

 

Steve - Intel Developer Support

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

 

 

What is Len_Trim(cwd)?

David

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

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

 

There is no project setting relevant to this.

Steve - Intel Developer Support

Quote:

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

www.quickthreadprogramming.com

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.

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.

Steve - Intel Developer Support

发表评论

登录添加评论。还不是成员?立即加入