Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Fortran Fortran : generating different output files in a loop

  1. Jun 30, 2015 #1
    Code (Fortran):

    implicit none
      character*4 head1,head2,ext
      character*10 fn1,fn2
      integer i,kev

      do i=1, 50000
      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')
      write(3,*) i,i**2.
      write(4,*) i,i**3.
      if(mod(i,50).eq.0) then

    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 :
    Can anyone help me?
    How should I correct my code?
    Thank you,
  2. jcsd
  3. Jun 30, 2015 #2


    Staff: Mentor

    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.
  4. Jun 30, 2015 #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...
  5. Jun 30, 2015 #4


    Staff: Mentor

    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.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook