| Thread Closed |
Fortran - Output File names |
Share Thread |
| Jan30-09, 07:44 AM | #1 |
|
|
Fortran - Output File names
Hi,
I looked around in some forums, but couldn't find a good answer to this problem: my programmes produce several output files. When the programm is finished, I add certain simulation parameters to the file name, so that I see later, to which simulation run this file belongs. (for example, a file "result.dat" is renamed to "result_500.dat" because the 500 is a certain parameter in that programm). Now i don't want to manually rename a lot of files, but instead I'm looking for a way to make Fortran incorporate certain variables in the file names. I only know this way of declaration: Code:
open(unit=1,status='unknown',file='result.dat') Is it possible to assign a file name "result_*number*.dat", so that I only have to change the value of "number" and all files have that in their names? Thanks for your help. |
| Jan30-09, 08:39 AM | #2 |
|
|
A possible solution , say you have 10 files with differnet names and you want to add the number 19 to all of them . Put the initial names(i.e., before adding 19) of these files in a character array defined as follow:
CHARACTER(len=25),DIMENSION(10)::filenames also, define a character that contains the number(19) CHARACTER(len=2)::number number='19' !The quotqtion are important then to add the number to the names use the concatenation operator as follows: DO i=1,10 filename(i)=filaename(i) // number// '.dat' END DO now you have the names ready in the array, you can open ten files now with the statement: DO i=1,10 OPEN(UNIT=i+10,FILE=filename(i),STATUS='replace',ACTION='write') END DO Now you have ten files with the required names ready to write in them. May be this is not the best way, but I hope it works for you. |
| Jan31-09, 04:26 AM | #3 |
|
|
Thanks for your answer! That's exactly what I'm looking for.
I'm still having a little trouble though, because the numbers that should be in the filenames are integer values and I don't know how to convert them into characters. It's supposed to work with the simple write - command, but somehow I can't get it to work correctly :( |
| Jan31-09, 11:59 AM | #4 |
|
|
Fortran - Output File names
Hi angura,
You can use an internal file to do this. Say we have an integer i=567, and a character called number and we want to assign the value of i to the character then this piece of code will do the job: .......... INTEGER::i CHARACTER(len=10)::number i=567 WRITE(number,100) i 100 FORMAT(I3) ........ A couple of comments, check that the length of the character is enough since this can create a runtime error. Also note that the write statement above creates a buffer file and do the conversion . |
| Jan31-09, 03:51 PM | #5 |
|
|
Take care that the number has three digits when you use format I3.
If you want numbers with a maximum of three digits, with leading zeroes for those below 100, you can use the Iw.m conversion, for example, Code:
WRITE(number,100) i 100 FORMAT(I3.3) This is important because you do not want a file name like: ACE 7.dat but rather ACE007.dat |
| Jan25-10, 09:18 PM | #6 |
|
|
I also have the same problem.
However, the answer doesn't work to me. I used gfortran to compile, then it said filename must be scalar. How can I fix this? or should I use another compiler? |
| Jan27-10, 05:48 AM | #7 |
|
|
At which position does it complain, that filename should be scalar?
Could you post a minimal example of your code? |
| Jan27-10, 07:41 AM | #8 |
|
Recognitions:
|
I typically write to a variable, using variable and strings, which I then open. Here is an example from a subroutine I have:
Code:
DO m=1,nFiles WRITE(fname,'(a,i4.4,a)')'grid',m,'.fast' OPEN(12,file=fname,form='formatted') ... ... END DO Code:
oname = 'post.'//fname WRITE(6,*) 'OPENING: ',oname OPEN(13,file=oname,file='formatted') Code:
DO i=1,10 OPEN(i,file='whatever') END DO |
| Thread Closed |
Similar discussions for: Fortran - Output File names
|
||||
| Thread | Forum | Replies | ||
| Fortran - How to retrieve read near middle or end of large file quickly. | Programming & Comp Sci | 2 | ||
| Fortran output file | Programming & Comp Sci | 1 | ||
| Problems reading binary file in FORTRAN | Programming & Comp Sci | 1 | ||
| LaTeX output not copied to FILE ! Why? | Math & Science Software | 17 | ||