Fortran Fortran : generating different output files in a loop

Click For Summary
The Fortran code aims to generate 5,000 output files named sequentially, but it encounters a runtime error due to incorrect file handling. The issue arises from the use of the loop index in the conditionals, which should check for "mod 50 equals 1" instead of "mod 50 equals 0" to ensure files are opened and written correctly. Additionally, the format specification for the variable 'kev' should be adjusted to "i4.4" to include leading zeros in filenames. There are concerns about writing to non-existent files, as many iterations do not meet the conditions to open the files, potentially leading to default output files. Proper file management and character variable length adjustments are necessary for successful execution.
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.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

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