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

[Fortran} error in formatted

  1. May 4, 2014 #1
    Hi all,

    I got this error:

    Code (Text):

    rm -f *.o *.mod *.MOD *.exe *.stackdump main
    gfortran -g  -I/usr/include -c main.f90
    gfortran -g  -I/usr/include -c subroutines.f90

    10  format(data,'i3.3','.dat')  
    Error: Positive width required in format specifier D at (1)

        write (filename,10) a
    Error: FORMAT label 10 at (1) not defined
    make: *** [subroutines.o] Error 1
    ./runcase.sh: line 11: ./main: No such file or directory

    after executing below code:

    Code (Text):

    subroutine outputtest
    implicit none

     integer :: a,io, i,j,k,ierror
     double precision, dimension (500,500) :: x,y,z
     character (len=20) :: filename

      open(200,file=filename, status = 'new', action = 'write', iostat = ierror)
      do a = 1,20
        write (filename,10) a
        open (100,file = filename)
        do i = 1,299
          do j = 1,299
            write (200,*) x(120,120),y(120,120),z(120,120)
          end do
        end do          
      !  write(*,*) a
    10  format(data,'i3.3','.dat')  
      end do

     end subroutine


    what is that mean of specifier D?

    thank you
  2. jcsd
  3. May 5, 2014 #2


    Staff: Mentor

    I can't tell what you're trying to do.

    In the first executable statement, you're opening a file to write in, and associating unit number 200. Any writes to the file should have a first parameter of 200, the unit number. Since you have not initialized your filename string, when you open the file, the program give it some random name.

    In the DO loop, the first write statement should be write(200, 10) a. This assumes that you want to write the values 1, 2, 3, ..., 19, and 20 to unit 200. The format statement whose label is 10 is not written correctly. Since all you're doing is writing an integer value to the file, it should be something like this:
    10 format (i5)

    You have another open statement inside the DO loop. Do you really mean to open a second file?

    Inside the nested DO loops you are writing to unit 200, the first file you opened, and are writing the same three values in the file; namely x(120, 120), y(120, 120), and z(120, 120). These will be garbage values, as you have not initialized any of these arrays. That write statement executes almost 90,000 times, but it writes the same three garbage values for each of these times.

    It would be good for you to spend some time studying fortran syntax, such as opening files, how print and format statements work, among other things.
  4. May 5, 2014 #3
    Actually, I have 20 .dat files containing (x,y,z). From each files, I need to extract data from row (i,j) = (120,120), and the value from all the files will be write into one new file. I am really not an expert in fortran. Please guide me.

    thank you
  5. May 5, 2014 #4


    Staff: Mentor

    To extract data, you need to READ from the files, not WRITE to them.

    How is the data arranged in each of the files? Data is not stored in files as tables. Is the file a binary file or a text file?

    Some pseudocode...
    Code (Text):

    Open a file for writing.
    For each of the 20 data files
       Construct a string that contains the name of one of the input data files.
       Open the file for reading.
       Locate to the correct position in the file. (*)
       Read the data from the input file.
       Write the value to the output file.
    End for
    (*) The details of the step with the asterisk depend on how the data is stored in the input files.

    As I said before,
    Also how READ works. I didn't understand before what you were trying to do.

    I've seen a lot of your posts over the past almost three years, and it seems that many or even most of them were questions about fortran code. Most of the mistakes you are making would be preventable if you put in some time at learning fortran.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook