Reputation: 5068
I'm trying to create the full path of a file, entering the parent folder in via a Read
statement:
PROGRAM fileTest
IMPLICIT NONE
Character(LEN=20) :: dirpath,fullpath
Write(6,*) 'Enter dir path'
Read(*,'(a)') dirpath
dirpath=TRIM(dirpath)
fullpath=dirpath//'/file.abc'
print*,fullpath
END PROGRAM fileTest
Using gfortran, the code compiles, but entering /home/chris
results in the final print statement still giving
/home/chris /file.abc
(note the 9 characters of whitespace).
How do I get rid of the spurious whitespaces?!
Upvotes: 1
Views: 4459
Reputation: 2953
This happens because dirpath
is still a character(len=20)
variable, so its contents are again padded with spaces after dirpath=TRIM(dirpath)
. You have to do the trimming like this:
fullpath = trim(dirpath)//'/file.abc'
Edit:
As a demonstration of allocatable strings (see my comment), you should be able to handle arbitrary string length more satisfactorily like such, provided your compiler supports this feature:
character(:), allocatable :: fullpath
character(len=some_length) :: buffer
write(6,*) 'Enter dir path'
read(*,'(a)') buffer
fullpath = trim(buffer) // '/file.abc'
The string fullpath
should be automatically allocated to accommodate the exact length of the right-hand side.
Upvotes: 3