|
|
 |
 |
 |
 |
Fortran Programming Language
|
 |
 |
 |
 |
 |
 |
 |
 |
concatenation for a filename
I seek to write a program that opens a modest number of files, all of which have the form "m23.txt" or "m78.txt". How would a person make a concatenation between m and the integers that are supplied by a looping variable, such that it would satisfy the filename attribute for an open statement? -- Wade Ward
Wade Ward <zaxf @gmail.com> wrote: > I seek to write a program that opens a modest number of files, all of > which have the form "m23.txt" or "m78.txt". How would a person make a > concatenation between m and the integers that are supplied by a > looping variable, such that it would satisfy the filename attribute > for an open statement? This one counts as a frequently asked question. The answer is to use an internal write, as in character*7 file_name integer :: file_number ... write(file_name,'(a1,i2.2,a4)') 'm', file_number, ',txt' or any number of variants. For example, if you want do the conversion of file_number to a string as a separate step and then concatenate, you could do character*2 string ... write (string,'(i2.2)') file_number file_name = 'm' // string // '.txt' Look up internal write in any Fortran reference for more details. Note the i2.2. That's a common usage in situations like this. It uses leading zeros as needed to pad to 2 characters so that a file number such as 7 turns into 'm07.txt' instead of 'm 7.txt'. The blank in the middle of that last form can be problematic; the 07 is much nicer for several reasons. -- Richard Maine | Good judgement comes from experience; email: last name at domain . net | experience comes from bad judgement. domain: summertriangle | -- Mark Twain
Wade Ward wrote: > I seek to write a program that opens a modest number of files, all of > which have the form "m23.txt" or "m78.txt". How would a person make a > concatenation between m and the integers that are supplied by a > looping variable, such that it would satisfy the filename attribute > for an open statement? > -- > Wade Ward
-- program demo character*64 fname integer n do n = 1,99 write(fname, 100) n 100 format('m',I2.2,'.txt') print *, fname enddo end -- cheers, Rich
In article <f42fe0$e0@scrotar.nss.udel.edu>, Rich Townsend <r@barVOIDtol.udel.edu> wrote: >Wade Ward wrote: >> I seek to write a program that opens a modest number of files, all of >> which have the form "m23.txt" or "m78.txt". How would a person make a >> concatenation between m and the integers that are supplied by a >> looping variable, such that it would satisfy the filename attribute >> for an open statement?
(Program using internal write omitted.) If you want to do it without an internal write, it can be done in f95. Error-trapping for n or size out of range is an exercise for the user: MODULE int2char ! i2c(n,size) converts integer n ( n>=0, n<10**size ) to character*size INTEGER, PRIVATE,PARAMETER:: i0 = iachar('0') CHARACTER,PRIVATE,PARAMETER:: cdigit(0:9) = (/(achar(n),n=i0,i0+9)/) CONTAINS FUNCTION i2c( n,size) ! e.g. i2c(1,2) is '01' INTEGER,INTENT(IN):: n,size CHARACTER(size) :: i2c INTEGER :: digit,signless,i signless = abs(n) DO i = size,1,-1 digit = mod(signless,10) ! one of 0 1 ... 9 signless = (signless - digit)/10 i2c(i:i) = cdigit(digit) END DO END FUNCTION i2c END MODULE int2char PROGRAM digits2char USE int2char INTEGER i PRINT '(A)', ('m'//i2c(i,2)//'.txt',i=0,99) END PROGRAM digits2char -- John Harper, School of Mathematics, Statistics and Computer Science, Victoria University, PO Box 600, Wellington 6140, New Zealand e-mail john.har@vuw.ac.nz phone (+64)(4)463 5341 fax (+64)(4)463 5045
John Harper wrote: > In article <f42fe0$e0 @scrotar.nss.udel.edu>, > Rich Townsend <r @barVOIDtol.udel.edu> wrote: >> Wade Ward wrote: >>> I seek to write a program that opens a modest number of files, all of >>> which have the form "m23.txt" or "m78.txt". How would a person make a >>> concatenation between m and the integers that are supplied by a >>> looping variable, such that it would satisfy the filename attribute >>> for an open statement? > (Program using internal write omitted.) > If you want to do it without an internal write, it can be done in f95. > Error-trapping for n or size out of range is an exercise for the user: > MODULE int2char > ! i2c(n,size) converts integer n ( n>=0, n<10**size ) to character*size > INTEGER, PRIVATE,PARAMETER:: i0 = iachar('0') > CHARACTER,PRIVATE,PARAMETER:: cdigit(0:9) = (/(achar(n),n=i0,i0+9)/) > CONTAINS > FUNCTION i2c( n,size) ! e.g. i2c(1,2) is '01' > INTEGER,INTENT(IN):: n,size > CHARACTER(size) :: i2c > INTEGER :: digit,signless,i > signless = abs(n) > DO i = size,1,-1 > digit = mod(signless,10) ! one of 0 1 ... 9 > signless = (signless - digit)/10 > i2c(i:i) = cdigit(digit) > END DO > END FUNCTION i2c > END MODULE int2char > PROGRAM digits2char > USE int2char > INTEGER i > PRINT '(A)', ('m'//i2c(i,2)//'.txt',i=0,99) > END PROGRAM digits2char > -- John Harper, School of Mathematics, Statistics and Computer Science, > Victoria University, PO Box 600, Wellington 6140, New Zealand > e-mail john.har@vuw.ac.nz phone (+64)(4)463 5341 fax (+64)(4)463 5045
Now I'm intrigued - given that this code is a) longer and b) less obvious than the version using internal write - is there a particular reason that doing it without might be advantageous? Or was it just a fun bit of code because you can? Catherine. -- Catherine Rees Lay Polyhedron Software Ltd. Registered Office: Linden House, 93 High St, Standlake, Witney, OX29 7RH, United Kingdom. Registered in England No.2541693. Vat Reg No. GB 537 3214 57
On Jun 6, 5:06 am, Catherine Rees Lay <catherine.n@polyhedron.com> wrote:
> John Harper wrote: > > In article <f42fe0$e0 @scrotar.nss.udel.edu>, > > Rich Townsend <r @barVOIDtol.udel.edu> wrote: > >> Wade Ward wrote: > >>> I seek to write a program that opens a modest number of files, all of > >>> which have the form "m23.txt" or "m78.txt". How would a person make a > >>> concatenation between m and the integers that are supplied by a > >>> looping variable, such that it would satisfy the filename attribute > >>> for an open statement? > > (Program using internal write omitted.) > > If you want to do it without an internal write, it can be done in f95. > > Error-trapping for n or size out of range is an exercise for the user: > > MODULE int2char > > ! i2c(n,size) converts integer n ( n>=0, n<10**size ) to character*size > > INTEGER, PRIVATE,PARAMETER:: i0 = iachar('0') > > CHARACTER,PRIVATE,PARAMETER:: cdigit(0:9) = (/(achar(n),n=i0,i0+9)/) > > CONTAINS > > FUNCTION i2c( n,size) ! e.g. i2c(1,2) is '01' > > INTEGER,INTENT(IN):: n,size > > CHARACTER(size) :: i2c > > INTEGER :: digit,signless,i > > signless = abs(n) > > DO i = size,1,-1 > > digit = mod(signless,10) ! one of 0 1 ... 9 > > signless = (signless - digit)/10 > > i2c(i:i) = cdigit(digit) > > END DO > > END FUNCTION i2c > > END MODULE int2char > > PROGRAM digits2char > > USE int2char > > INTEGER i > > PRINT '(A)', ('m'//i2c(i,2)//'.txt',i=0,99) > > END PROGRAM digits2char > > -- John Harper, School of Mathematics, Statistics and Computer Science, > > Victoria University, PO Box 600, Wellington 6140, New Zealand > > e-mail john.har@vuw.ac.nz phone (+64)(4)463 5341 fax (+64)(4)463 5045 > Now I'm intrigued - given that this code is a) longer and b) less > obvious than the version using internal write - is there a particular > reason that doing it without might be advantageous? Or was it just a fun > bit of code because you can? > Catherine.
In Fortran 95, if function f does an internal write, it is non- standard to write print*,f(x) That makes I/O more cumbersome, since one must write temp = f(x) print*,temp I think this restriction is removed in Fortran 2003.
On Jun 4, 9:42 pm, nos@see.signature (Richard Maine) wrote: > Wade Ward <zaxf @gmail.com> wrote: > > I seek to write a program that opens a modest number of files, all of > > which have the form "m23.txt" or "m78.txt". How would a person make a > > concatenation between m and the integers that are supplied by a > > looping variable, such that it would satisfy the filename attribute > > for an open statement? > This one counts as a frequently asked question.
Yes, and it is in the Wikibook Fortran FAQ http://en.wikibooks.org/wiki/Fortran_FAQ#Q:_How_do_I_create_numbered_... .
In article <0e2dnUaBppOH5PvbRVny@eclipse.net.uk>, Catherine Rees Lay <catherine.n@polyhedron.com> wrote: >John Harper wrote: >> In article <f42fe0$e0 @scrotar.nss.udel.edu>, >> Rich Townsend <r @barVOIDtol.udel.edu> wrote: >>> Wade Ward wrote: >>>> I seek to write a program that opens a modest number of files, all of >>>> which have the form "m23.txt" or "m78.txt". How would a person make a >>>> concatenation between m and the integers that are supplied by a >>>> looping variable ... >> If you want to do it without an internal write, it can be done in f95.
(code omitted) >Now I'm intrigued - given that this code is a) longer and b) less >obvious than the version using internal write - is there a particular >reason that doing it without might be advantageous? Or was it just a fun >bit of code because you can?
I must admit it was a fun bit of code to write, but there are also two valid objective reasons why it was worth doing. 1. With some compilers I found that my method runs faster, with others the internal write does, so it's worth trying both if speed matters. 2. In f95 the function using my method can be used in a print or write statement, but one using an internal write can't (recursive I/O). Of course f2003 doesn't have that restriction, but some people still use f77 compilers now, so I suspect that compilers implementing the restriction may be in use for some time. -- John Harper, School of Mathematics, Statistics and Computer Science, Victoria University, PO Box 600, Wellington 6140, New Zealand e-mail john.har@vuw.ac.nz phone (+64)(4)463 5341 fax (+64)(4)463 5045
On Jun 6, 6:01 pm, har@mcs.vuw.ac.nz (John Harper) wrote:
> In article <0e2dnUaBppOH5PvbRVny @eclipse.net.uk>, > Catherine Rees Lay <catherine.n @polyhedron.com> wrote:>John Harper wrote: > >> In article <f42fe0$e0 @scrotar.nss.udel.edu>, > >> Rich Townsend <r @barVOIDtol.udel.edu> wrote: > >>> Wade Ward wrote: > >>>> I seek to write a program that opens a modest number of files, all of > >>>> which have the form "m23.txt" or "m78.txt". How would a person make a > >>>> concatenation between m and the integers that are supplied by a > >>>> looping variable ... > >> If you want to do it without an internal write, it can be done in f95. > (code omitted) > >Now I'm intrigued - given that this code is a) longer and b) less > >obvious than the version using internal write - is there a particular > >reason that doing it without might be advantageous? Or was it just a fun > >bit of code because you can? > I must admit it was a fun bit of code to write, but there are also two > valid objective reasons why it was worth doing. > 1. With some compilers I found that my method runs faster, with others > the internal write does, so it's worth trying both if speed matters. > 2. In f95 the function using my method can be used in a print or write > statement, but one using an internal write can't (recursive I/O). > Of course f2003 doesn't have that restriction, but some people still > use f77 compilers now, so I suspect that compilers implementing the > restriction may be in use for some time.
Thanks all for responses. I'll drag them back to my machine and see what happens. Richard suggested that I look up an internal write in any Fortran reference. I think it's high time that I widened the Fortran holdings on my shelf to more than one book. Recommendations? -- Wade Ward
As if by magic, Wade Ward appeared ! > Thanks all for responses. I'll drag them back to my machine and see > what happens. Richard suggested that I look up an internal write in > any Fortran reference. I think it's high time that I widened the > Fortran holdings on my shelf to more than one book. Recommendations?
Metcalf, Reid and Cohen, "Fortran 95/2003 Explained," Oxford University Press, ISBN 0-19-852693-8 Ian
|
 |
 |
 |
 |
|