- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi there
Could someone let me know how to write several times free-format outputs on the same line.
Many thanks
Hamid
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi there
Could someone let me know how to write several times free-format outputs on the same line.
Many thanks
Hamid
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
program main
integer*8 a,b,c
real*8 u,v,w
data a,b,c,u,v,w/1,2,3,4.5678,3.42145,2.78934/
write(*,10) a,b,c,u,v,w
10 format('Integers=',3(1x,i5),2x,'real numbers=',3(2x,f6.4))
end program
Is this what you are looking for?
Hope it helps
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thank you peter.
In the following example, the program prints 10 times the inputted integer number.
integer I, a
5 read*,a
do I = 1, 9
write (*, 10, advance='no') a
enddo
! New line to complete
write (*, 10) a
10 format (9I2)
go to 5
stop
end
But how can I do thatwith a free format writing?
Hamid
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
this can be done by
project>add new item>fortran file(.f90)
when i use your code i get results as follows
1
1 1 1 1 1 1 1 1 1
2
2 2 2 2 2 2 2 2 2
is this what you are looking for?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
this can be done by
project>add new item>fortran file(.f90)
when i use you code iget results as follows
1
1 1 1 1 1 1 1 1 1
2
2 2 2 2 2 2 2 2 2
is this what you are looking for?
Yes it is, but I get each one ten times.
When I said free format, I meant using print *,or write(*,*).
How can I add the free format in Visual Studio?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
program main
integer i,a,j
dimension a(9)
do i=1,9
read(*,*) a(i)
end do
do j=1,9
write(*,10) (a(i),i=1,9)
10 format(9(1x,i2))
end do
end
this is modern version of your code. by the way what compiler are you using?.try not to use 'go to' statements often. That hasbeen outdated.
unfortunately you have to use format statement to make you output more understandable.
that is why i've used format.
Hope this helps!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
program main
integer i,a,j
dimension a(9)
do i=1,9
read(*,*) a(i)
end do
do j=1,9
write(*,10) (a(i),i=1,9)
10 format(9(1x,i2))
end do
end
this is modern version of your code. by the way what compiler are you using?.try not to use 'go to' statements often. That hasbeen outdated.
unfortunately you have to use format statement to make you output more understandable.
that is why i've used format.
Hope this helps!
Many thanks Peter, but you did not use free format writing!
The Program I posted was just a simple example.
In my main program I need to write many items on a line to input easilyinto a spread sheet. The thing is I need to use free format.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Unfortunately or as far as i know , you have to use format statement when you want to analyse large data using excel or matlab. Because format statement controls your output. If it is free format 'fortan' decides what to do, but if you use 'format' you decide how the output should be.
However large the code be youmight wannabe comfortable geting used to 'format' statement, in my opinion.
I recommend you use matlab, if you have one and have been using it already!
Let me know if you have any questions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Unfortunately or as far as i know , you have to use format statement when you want to analyse large data using excel or matlab. Because format statement controls your output. If it is free format 'fortan' decides what to do, but if you use 'format' you decide how the output should be.
However large the code be youmight wannabe comfortable geting used to 'format' statement, in my opinion.
I recommend you use matlab, if you have one and have been using it already!
Let me know if you have any questions
The thing is that the number of the digits before and after the decimal point varies and that is why I am avoiding the F or E formats.
Thanks very much for your help but let's wait more as someone might know how to do thatwith free format.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The thing is that the number of the digits before and after the decimal point varies and that is why I am avoiding the F or E formats.
Thanks very much for your help but let's wait more as someone might know how to do thatwith free format.
I would be excited to see if someone gives an alternative option.
However you can control the number of decimal points. For example if you say f7.5 for a real(4) declaration, and lets say the number be 1.23456, the number of digits are 6, however if you want to get all you numbers after the decimal printed give f(6+1).5==f7.5, so you'l get output as 1.23456.
Again this is when you know the data that you input. If you are generating some values it is no harm to give extra width, say f10.5. I think you should spend some time playing with formats and be a good programmer.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Unfortunately or as far as i know , you have to use format statement when you want to analyse large data using excel or matlab. Because format statement controls your output. If it is free format 'fortan' decides what to do, but if you use 'format' you decide how the output should be.
However large the code be youmight wannabe comfortable geting used to 'format' statement, in my opinion.
I recommend you use matlab, if you have one and have been using it already!
Let me know if you have any questions
If you output the numbers with CHAR(9) in between each one, you can produce a "tab-delimited" text file which Excel will easily read in and put all the values into the right cells.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
If you output the numbers with CHAR(9) in between each one, you can produce a "tab-delimited" text file which Excel will easily read in and put all the values into the right cells.
David,
I agree with you regarding char(9).This helpsuser to avoid 'format' statement.
The disadvantage I see is
1. Not compatible with type 'real'
Do you agree David ?
Haminin,
I still can not achieve print*, or flat 'write(*,*)' , because eventually, I believe, you have to write data into an external file.
In this context I used character(8) for printing numbers 1 - 9
[cpp]program main integer i,j,a character(8) f(9) dimension a(9) f=char(a(9)) do i=1,9 read(*,*) f(i) end do open(3,file='out.txt',status='unknown') do i=1,9 write(3,*), (f(j),j=1,9) end do end[/cpp]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
If you output the numbers with CHAR(9) in between each one, you can produce a "tab-delimited" text file which Excel will easily read in and put all the values into the right cells.
But it goes to the next line after writing a few numbers.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
David,
I agree with you regarding char(9).This helpsuser to avoid 'format' statement.
The disadvantage I see is
1. Not compatible with type 'real'
Do you agree David ?
Haminin,
I still can not achieve print*, or flat 'write(*,*)' , because eventually, I believe, you have to write data into an external file.
In this context I used character(8) for printing numbers 1 - 9
[cpp]program main integer i,j,a character(8) f(9) dimension a(9) f=char(a(9)) do i=1,9 read(*,*) f(i) end do open(3,file='out.txt',status='unknown') do i=1,9 write(3,*), (f(j),j=1,9) end do end[/cpp]
It still goes to the nextline for example when "12" is used instead of "9".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Haminin
There is some confusion.
char(a(9)) is not the same as char(9)
Youdon't really needto use the charactervariable f but if you do then it needs to be
character(1) ::f (that is one character)
f = char(9)
Unfortunately your READ statement then overwrites the contents of "f" ! I presume you meant to read into the array "a"
You then wrote the contents of "f" nine times - that is nine lines each with the same nine values stored in "f"
what you need is a write that will outputthe firstnumber followed by a char(9) (that is a TAB character) then the next number followed by a char(9), and so on.
do i = 1,n
write(*,*) a(i),char(9)
....
will outputa number followed by a TAB (you may use variable "f" in place of the char(9) if you have made the changes I describe above.
In order to output everything on one line you could write
write(3,*) ((a(i),char9)),i=1,9)
note if you only have an array of 9 items you don't need the surrounding do loop, the implied do loop in the write statement is enough.
but you will need to consider the line length of the output record
because advance="no" is not allowed with list directed format
Les
minor correction of terminology
what you call "free format" is actually "List Directed" format and is to do with I/O
"free format" would usually be understood as referring to source code
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
because advance="no" is not allowed with list directed format
Many thanks Les.
So it seems impossible to output several free-format data on one line.
Using write(3,*) ((a(i),char9)),i=1,9) does not print everything on one line as you said it depends on the line length.
Hamid
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I would agree with others that using list-directed output is not recommended when you care what the output looks like. An explicit format is much better for this and you will have full control over where lines begin. The G format item can be used for values of any type.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Many thanks Les.
So it seems impossible to output several free-format data on one line.
Using write(3,*) ((a(i),char9)),i=1,9) does not print everything on one line as you said it depends on the line length.
Hamid
From experience, Excel uses double precision values (but with an E exponent), so I allow about 15 digits per value for Excel (The reason I do this is that I also export from Excel and read it back in using Fortran).
This means that allowing 15-20 characters per value, i.e. upto 180 for 9 values should be enough.
So this gives ...
OPEN(UNIT=3, FILE="MYFILE", RECL=180)
WRITE(3,*) ((A(i),CHAR(9)),i=1,9)
or similar.
Regards,
David
P.S., I can follow up with some of my actual code, but can't do this for about a week.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
From experience, Excel uses double precision values (but with an E exponent), so I allow about 15 digits per value for Excel (The reason I do this is that I also export from Excel and read it back in using Fortran).
This means that allowing 15-20 characters per value, i.e. upto 180 for 9 values should be enough.
So this gives ...
OPEN(UNIT=3, FILE="MYFILE", RECL=180)
WRITE(3,*) ((A(i),CHAR(9)),i=1,9)
or similar.
Regards,
David
P.S., I can follow up with some of my actual code, but can't do this for about a week.
David,
But the problem comes when, if you want to try printing 1-12
Can you please check this example code.
[cpp]program main integer i,a,j character(1) f dimension a(12) f=char(9) do i=1,12 read(*,*) a(i) end do open(unit=3,file='out.txt',status='unknown',recl=1024) do i=1,12 write(3,*) (a(i),char(9), j=1,11) end do end[/cpp]I know there is nothing as good as using list directed output, but I still believe there must be a solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
This is a modification of my previous code. So now it prints 1-12 numbers on the same line. The output looks like this
1 1 1 1 ......
2 2 2 2 ......
This can be done by changing
[cpp]write(3,*) (f,a(i),j=1,11)[/cpp]Let me know if this works for you!

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