Fortran : generating different output files in a loop

Click For Summary

Discussion Overview

The discussion revolves around a Fortran code snippet intended to generate multiple output files in a loop. Participants are addressing issues related to file naming, writing to files, and runtime errors encountered during execution. The focus is on debugging and improving the code's functionality.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant points out that the current if statements only execute after the loop index reaches 50, suggesting a modification to check for the index being equal to one mod 50 instead.
  • Another participant comments on the format specification for the variable 'kev', recommending "i4.4" to ensure proper padding of file names with leading zeros, which could prevent issues with file naming.
  • Concerns are raised about writing to non-existent files due to the placement of write statements outside the if conditions, which could lead to default files being created instead.
  • There is a suggestion that more unit numbers may be needed for opening files, as the current use of units 3 and 4 might not suffice for the intended operations.
  • Participants express uncertainty about the intent behind writing file names to units 3 and 4, questioning its purpose in the context of the code.

Areas of Agreement / Disagreement

Participants generally agree that there are issues with the code that need addressing, but multiple competing views on how to correct these issues remain. The discussion is unresolved regarding the best approach to fix the code.

Contextual Notes

Limitations include potential misunderstandings of the intended file operations, the need for additional unit numbers, and the character length of file names not being adequately accounted for.

Kyong
Messages
3
Reaction score
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
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.
 
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...
 
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.
 

Similar threads

  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K