Fortran : generating different output files in a loop

In summary, the code is compiled and ran, but it gave an error when trying to write to the files. The length of the character variables is not sufficient, and each file needs an OPEN statement to be opened before writing to it.
  • #1
Kyong
3
0
Fortran:
implicit none
  character*4 head1,head2,ext
  character*10 fn1,fn2
  integer i,kev

  head1='jack'
  head2='jill'
  ext='.dat'
  do i=1, 50000
  kev=int((i-1)/50)+1000
  if(mod(i,50).eq.0) then
  write(fn1,'(a,i4,a)') head1,kev,ext
  write(3,*) fn1
  write(fn2,'(a,i4,a)') head2,kev,ext
  write(4,*) fn2
  open (unit=3,file=fn1,status='unknown')
  open (unit=4,file=fn2,status='unknown')
  endif
  write(3,*) i,i**2.
  write(4,*) i,i**3.
  if(mod(i,50).eq.0) then
  close(3)
  close(4)
  endif
  enddo

  end

I want to write a code that can generate five thousand output files with jack1001.dat, jill1001.dat, ..., jack6000.dat, jill6000.dat...This code is compiled well without any error or warning message but if running the produced executable file, it showed error message :
At line 12 of file test3.f
Fortran runtime error: End of record

Can anyone help me?
How should I correct my code?
Thank you,
 
Technology news on Phys.org
  • #2
One problem is that both of your if statements test if the loop index mod 50 is ZERO to write some stuff and open some files won't execute until the index is 50. I think you should test if it's equal to ONE mod 50.
 
  • #3
Your format specification for kev, instead of " i4 " it needs to be " i4.4 " ...this will cause numbers less than 1000 to be padded with leading zeros, like " jack0050.ext"; otherwise you will end up with a disconnected string for a file name with blank spaces in it...not cool at all, in my very personal opinion...I know that GUI oriented people don't care, but I don't remember last time I used a file manager...I am a command line kind of guy.

Other than that, I am not sure what you intent on writing to the files, to be sure, you have a couple of write statements outside the " if " statements and will write to non-existent files most of the time (49 out of 50 times)...will probably create default fort3 and fort4 files or something like that. Depending on what you are trying to do, I am thinking you need more unit numbers than just 3 and 4.

It does not seem to make sense to write the name of the files to units 3 and 4...what is that supposed to achieve?

The length of your character variables fn1 and fn2 are a bit short...count the characters you are planning to write to them...

...bla, bla, bla...
 
  • #4
gsal said:
Other than that, I am not sure what you intent on writing to the files, to be sure, you have a couple of write statements outside the " if " statements and will write to non-existent files most of the time (49 out of 50 times)...will probably create default fort3 and fort4 files or something like that. Depending on what you are trying to do, I am thinking you need more unit numbers than just 3 and 4.
I'm thinking that as well. Also, each file you write you will need an OPEN statement to open it before you write to it.

Similar questions have been asked here before. Check out the links to similar discussions, below.
 

1. How do I generate multiple output files in a loop in Fortran?

To generate multiple output files in a loop in Fortran, you can use the OPEN statement with a variable for the file name. Inside the loop, you can use the WRITE statement to write data to the file and then close the file with the CLOSE statement.

2. Can I specify the file name for each output file in the loop?

Yes, you can specify the file name for each output file in the loop by using a variable for the file name in the OPEN statement. You can also use string concatenation or other methods to create unique file names for each iteration of the loop.

3. How can I ensure that each output file is saved to a different directory?

You can ensure that each output file is saved to a different directory by including the directory path in the file name variable used in the OPEN statement. You can also use the SYSTEM function to create directories within the loop if needed.

4. Is it possible to generate output files with different formats in the loop?

Yes, it is possible to generate output files with different formats in the loop. You can use different OPEN statements with different FORMAT specifications for each file, or you can use conditional statements within the loop to determine the format of each output file.

5. Can I use Fortran's intrinsic functions to manipulate the data before writing to the output files?

Yes, you can use Fortran's intrinsic functions to manipulate the data before writing to the output files. You can use these functions within the WRITE statement or store the manipulated data in a variable before writing it to the file.

Similar threads

  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
Back
Top