Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strings in fortran and filenames using them

Tags:

string

fortran

here is the trouble ... i'm dynamically building (rather changing)a string which contains numerals(numbers) (like to have filename out01.txt ,out02.txt etc ..)

my program works fine (i'm using the last updated value string to name a file and edit that file) ... but in the same directory with "ls" command, i can see that file created and through file browser i can acess it but from command line using vim , gedit i can't open it new file of same name is opening... moreover i can't remove that file from command line (rm out010.txt' no such file or directory) here is the code , i might not have been able to explain my problem but code will speak for itself ...

program strtest
implicit none
 character(len=1024)::filen,format_str
 integer::i
 format_str="(a5,i0.3,'.txt')"
 do i=1,10 
  write(filen,format_str)'out',i
 end do
write(*,*)trim(filen)
open(23,file=trim(filen))
write(23,*)"what a mess!"
close(23)
stop
end program strtest

note: i have the same problem even without using trim() function in file opening statement

please explain my situation!!

regards ...

like image 900
fedvasu Avatar asked Mar 24 '11 09:03

fedvasu


2 Answers

Your filenames are coming out with 2 spaces in front of them, so if you put rm " out01.txt" (2 spaces,out01.txt) you will be able to delete them. It's the a5 that's throwing off the format string.

like image 151
jonsca Avatar answered Oct 08 '22 22:10

jonsca


As @jonsca pointed out already, the problem is with the extra whitespace. The simplest way to get rid of it is to use adjustl, like this:

open(23,file=trim(adjustl(filen)))
like image 29
ev-br Avatar answered Oct 08 '22 22:10

ev-br