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

  • Thread starter Thread starter s_hy
  • Start date Start date
  • Tags Tags
    Error Fortran
AI Thread Summary
The discussion revolves around a Fortran programming error encountered during compilation, specifically related to format specifiers and file handling. The user receives errors indicating issues with the format statement and undefined labels in their code. Key points include the need to correctly initialize the filename variable before opening a file, as well as the correct usage of format statements for writing data. It is suggested that the user should write to unit 200 using the correct format and that the nested file opening within a loop may not be necessary. Additionally, there is a misunderstanding about the purpose of the code; instead of writing to files, the user needs to read data from existing files and then write specific values into a new file. Guidance emphasizes the importance of understanding Fortran syntax, particularly regarding file operations and data handling, to avoid common mistakes.
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 1 person
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.

Similar threads

Replies
5
Views
5K
Replies
8
Views
6K
Replies
3
Views
3K
Replies
8
Views
4K
Replies
2
Views
1K
Replies
4
Views
9K
Replies
8
Views
18K
Replies
6
Views
2K
Back
Top