- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I have a small question.
I need to output N numbers, whose format is specified in format.
N is a variable, although I define it before I use it in the format. The compiler doesn't allow me to do this.
So I want to know how can I use N to control the output number of data, which need to be formatted and output in a line
I paste my wrong code below:
N=10
12 FORMAT (N(d15.6,2X))
WRITE (15,12) (X(I),I=1,N)
I need to output N numbers, whose format is specified in format.
N is a variable, although I define it before I use it in the format. The compiler doesn't allow me to do this.
So I want to know how can I use N to control the output number of data, which need to be formatted and output in a line
I paste my wrong code below:
N=10
12 FORMAT (N(d15.6,2X))
WRITE (15,12) (X(I),I=1,N)
Link Copied
4 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
In this case, if you change N in the format to the largest number you want per line, it should work as you requested. You could look up "Fortran format reversion."
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
If you did look up "format reversion", which is worthwhile, you would find that it does not do what you want. The reason is that if you omitted the N, then each value would be output on a separate line. When a format "reverts", a new record is started.
There are several approaches you can take to this. One which works in Intel Fortran but is an extension, not widely implemented, is to write your format as:
12 FORMAT ((D15.6,2X))
This extension is called "variable format expressions".
Another approach which is standard conforming is to write it this way:
12 FORMAT (10000(D15.6, 2X))
This assumes that N is never greater than 10000 and that the compiler you use allows a group repeat count to be as large as 10000. (All that I know of do.) This works because when the I/O list is exhausted, processing will stop the next time it hits the D15.6 format.
The draft standard for Fortran 2008 introduces a better way, called "unlimited format item". With that feature, which Intel Fortran does NOT yet support, you'd write:
12 FORMAT (*(D15.6,2X))
This would have the desired effect.
There are several approaches you can take to this. One which works in Intel Fortran but is an extension, not widely implemented, is to write your format as:
12 FORMAT (
This extension is called "variable format expressions".
Another approach which is standard conforming is to write it this way:
12 FORMAT (10000(D15.6, 2X))
This assumes that N is never greater than 10000 and that the compiler you use allows a group repeat count to be as large as 10000. (All that I know of do.) This works because when the I/O list is exhausted, processing will stop the next time it hits the D15.6 format.
The draft standard for Fortran 2008 introduces a better way, called "unlimited format item". With that feature, which Intel Fortran does NOT yet support, you'd write:
12 FORMAT (*(D15.6,2X))
This would have the desired effect.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thank you so much. The problem has been solved.
And the more important thing is that I can always learn something here ^ ^
And the more important thing is that I can always learn something here ^ ^
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Here isone way howyou can use variable format.
! FORMAT.f90
!
! FUNCTIONS:
!FORMAT - Entry point of console application.
!****************************************************************************
! PROGRAM: FORMAT
! PURPOSE: Example of setting and using variable formatS.
!****************************************************************************
program FORMAT
implicit none
INTEGER*4 N,I
INTEGER*4, PARAMETER :: NARRAY=32
REAL*8 ARRAY(NARRAY)
CHARACTER*5 FOURA8, I8
! Define a character array to contain the format string
CHARACTER*8 MYFORM(4)
! Divide the format up into four 8-character strings, with
! one of them (the second one here) left blank to be filled by the
! number you want to use
DATA MYFORM/8H( ,8H ,8H(D15.6, ,8H2X)) /
! MYFORM(3) and MYFORM(4) together specify 17 columns (15+2)
! so when the format is used, each number output causes 17 columns
! to be used. 5 or more of these will overflow an 80 column console so they
! will wrap around
! So, before you execute, set your DOS console window to 136 wide
! if you want to get 8*17 columns on one line in the console window
FOURA8='(4A8)'
I8='(I8)'
! Generate some numbers
DO I=1,NARRAY
ARRAY(I)=DBLE(I)
END DO
! SET THE NUMBER N TO THE VALUE YOU WANT, THEN
! SET THE SECOND ELEMENT OF THE FORMAT ARRAY TO THE NUMBER YOU WANT
! THEN WRITE THE FORMAT OUT TO INSPECT IT,
! THEN OUTPUT 8 LINES OF 4 NUMBERS USING THE FORMAT
N=4
WRITE(MYFORM(2),'(I8)') N
WRITE(6,'(4A8)') MYFORM
WRITE(6,MYFORM) ARRAY
! SET THE NUMBER N TO THE VALUE YOU WANT, THEN
! SET THE SECOND ELEMENT OF THE FORMAT ARRAY TO THE NUMBER YOU WANT
! THEN WRITE THE FORMAT OUT TO INSPECT IT,
! THEN OUTPUT 4 LINES OF 8 NUMBERS USING THE FORMAT
N=8
WRITE(MYFORM(2),I8) N
WRITE(6,FOURA8) MYFORM
WRITE(6,MYFORM) ARRAY
! SET THE NUMBER N TO THE VALUE YOU WANT, THEN
! SET THE SECOND ELEMENT OF THE FORMAT ARRAY TO THE NUMBER YOU WANT
! THEN WRITE THE FORMAT OUT TO INSPECT IT,
! THEN OUTPUT 2 LINES OF 16 NUMBERS USING THE FORMAT
N=16
WRITE(MYFORM(2),I8) N
WRITE(6,FOURA8) MYFORM
WRITE(6,MYFORM) ARRAY
end program FORMAT

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page