Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29253 Discussions

several outputs on the same line

haminin
Beginner
6,239 Views

Hi there

Could someone let me know how to write several times free-format outputs on the same line.

Many thanks

Hamid

0 Kudos
26 Replies
Les_Neilson
Valued Contributor II
988 Views
Quoting - Peter
do i=1,12
write(3,*) (a(i),char(9), j=1,11)
end do
Now I'm confused. :-)
What you have written is as follows

i=1
write out a(1) followed by a TAB, 11 times on the same line.
i=2
write out a(2) followed by a TAB, 11 times on the same line
and so on.

Do you really want the same number repeated 11 times along the same line ?

Note: Something I wanted to mention in my earlier reply but forgot.You could also replace char(9) with ',' to make the file a "comma separated value" file. This would have the advantage of making the separating character visible in a editor and/or printout of the file.

I am assuming that your real world problem is more complicated. Have you considered the possiblity of using an "internal write" to build a format string and then using that format for your write-to-file?

Les
0 Kudos
haminin
Beginner
988 Views
Quoting - Peter
Let me know if this works for you!

Posts 19 and 20 both work.

As Steve and David say, RECL needs change.

Many thanks

Hamid

0 Kudos
haminin
Beginner
988 Views

Do you really want the same number repeated 11 times along the same line ?

It is just an example and as you said the real problem is something else.

Have you considered the possiblity of using an "internal write" to build a format string and then using that format for your write-to-file?

I need to read about that. If it is easy to explain, please kindly do that in an example.

Many thanks
Hamid


0 Kudos
Les_Neilson
Valued Contributor II
988 Views
Quoting - haminin

Have you considered the possiblity of using an "internal write" to build a format string and then using that format for your write-to-file?

I need to read about that. If it is easy to explain, please kindly do that in an example.

Many thanks
Hamid




"As Steve and David say, RECL needs change."
I tried to hint at that in my first reply, alas my "hinting skills" need improving :-)

For internal I/O the process is along the following lines:

! declare a character string variable which will be used to hold the format
! ( note if the actual format to be used is more detailed than the one shown here
! it needs to be long enough to hold it)
character(80) :: my_fmt

! a character variable containing the separator
character :: csv

! an integer variable specifying how many numbers to write
integer :: n
! an integer variable specifying the calculated record length
integer :: recl

! loop counter
integer :: i
! Error status
integer :: ierr

! the array of data - size to be determined at run time
real, allocatable :: a(:)

! Initialise the separator
csv = ',' ! or could be char(9) for example

! Do something to get the number of values "n"
n = 42

! Using "n" allocate the array to receive the data
allocate(a(n), stat=ierr)

! Do something to read in the "n" values into array "a"
do i = 1,n
a(i) = i
enddo

! ===========================================
! This is the internal i/o part
! ===========================================
! Basically the "file unit" we write to is our character string "my_fmt"
! your actual format will depend on what you want to write
write(my_fmt, "(a1,i3,a16)") "(", n-1, "( f6.3, a),f6.3)"

! This will buildthestring my_fmt = "(41(f6.3,a),f6.3)"
! We will write out n-1 values and csv's followed by the final value

! calculate the record length
recl = (n-1)*7 + 6

open(3,file="out.txt",status="unknown",RECL=recl)

! Write to the file using our constructed format string
write(3,my_fmt) ((a(i),csv),i=1,n-1), a(n)


HTH

Les
0 Kudos
haminin
Beginner
988 Views
Quoting - Les Neilson
Les

Appreciated very much. It can be very helpful in the next step of my project.
Hamid

0 Kudos
Peter
Beginner
988 Views
Quoting - Les Neilson
Now I'm confused. :-)
What you have written is as follows

i=1
write out a(1) followed by a TAB, 11 times on the same line.
i=2
write out a(2) followed by a TAB, 11 times on the same line
and so on.

Do you really want the same number repeated 11 times along the same line ?

Note: Something I wanted to mention in my earlier reply but forgot.You could also replace char(9) with ',' to make the file a "comma separated value" file. This would have the advantage of making the separating character visible in a editor and/or printout of the file.

I am assuming that your real world problem is more complicated. Have you considered the possiblity of using an "internal write" to build a format string and then using that format for your write-to-file?

Les

Les,

In Hamid's first code he wants to print same number repeatedly along the same line. I was just trying to modify that code.
0 Kudos
Reply