What Does a Positive Width Error in Fortran Format Specifier Mean?

  • Context: Fortran 
  • Thread starter Thread starter s_hy
  • Start date Start date
  • Tags Tags
    Error Fortran
Click For Summary

Discussion Overview

The discussion revolves around an error encountered in Fortran related to format specifiers, specifically a "Positive width required in format specifier D." Participants explore the implications of this error, the correct usage of format statements, and the overall structure of the Fortran code provided by the original poster.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • The original poster describes an error related to a format specifier in their Fortran code and seeks clarification on its meaning.
  • One participant suggests that the format statement is incorrectly written and proposes an alternative format for writing integers.
  • Another participant questions the logic of opening a file within a loop and highlights potential issues with uninitialized arrays leading to "garbage values" being written.
  • A later reply emphasizes the need to read from files instead of writing, questioning the arrangement of data in the files and suggesting a pseudocode approach for extracting data.
  • Some participants express concern about the original poster's understanding of Fortran syntax and suggest that further study would be beneficial.

Areas of Agreement / Disagreement

Participants express differing views on the original poster's code structure and the correct approach to file handling in Fortran. There is no consensus on the best way to resolve the error or the overall intent of the code.

Contextual Notes

The discussion highlights limitations in the original poster's understanding of Fortran, particularly regarding file operations and format specifications. There are unresolved questions about the data format in the files being read.

Who May Find This Useful

Individuals learning Fortran, particularly those dealing with file I/O and format specifiers, may find this discussion relevant.

s_hy
Messages
57
Reaction score
0
Hi all,

I got this error:

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

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

    write (filename,10) a
                      1
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:
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
 
Technology news on Phys.org
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.
 
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
 
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:
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,
Mark44 said:
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.
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.
 
  • Like
Likes   Reactions: 1 person

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
Replies
8
Views
4K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 8 ·
Replies
8
Views
6K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
9K
  • · Replies 8 ·
Replies
8
Views
18K
  • · Replies 6 ·
Replies
6
Views
2K