Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to output Fortran program into the text file?

Status
Not open for further replies.

260791

MIS
Jun 14, 2010
16
GB
What's the best way to output all the WRITE(*,*) data from my program into a text file? I'm using Fortran 90/95

Thank you
 
Without changing the program code, you can redirect the standard output to the text-file.
Just run your program as follows:
Code:
your_program > output.txt
 
Is there anyway to output it to the text file by modifying the code?

Because I'd like to hide the code from the users. So that they will use the program and at the end there will be question: Would you like to see the output as a text file? And if they choose to do it, there will be a text file produced with the outputs.

 
I answered YWS to your question "You mean to type it in the command prompt?"

To your other question, how to write output in the text file - see this example:
Code:
[COLOR=#a020f0]program[/color] write2file
[COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
[COLOR=#0000ff]! open file[/color]
[COLOR=#804040][b]open[/b][/color] ([COLOR=#ff00ff]10[/color], [COLOR=#804040][b]file[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]'output_file.txt'[/color], [COLOR=#804040][b]status[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]'unknown'[/color])

[COLOR=#0000ff]! write to file[/color]
[COLOR=#804040][b]write[/b][/color]([COLOR=#ff00ff]10[/color], [COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'Hello World!'[/color]

[COLOR=#0000ff]! close file[/color]
[COLOR=#804040][b]close[/b][/color]([COLOR=#ff00ff]10[/color])
[COLOR=#a020f0]end program[/color] write2file
After compiling and running the program you will get the file output_file.txt which contains this
Code:
 Hello World!
 
Thanks for your help! I really appreciate that :)

What if I'd like to out put:

write(10,*)"-----------------------------------------------------------"
write(10,*)
write(10,*)"Flowrate:",aflowrate(1),"m3/hr, C1/C2:",ac1c2(1),"W:",adropset(1)
write(10,*)"Flowrate:",aflowrate(2),"m3/hr, C1/C2:",ac1c2(2),"W:",adropset(2)
write(10,*)"Flowrate:",aflowrate(3),"m3/hr, C1/C2:",ac1c2(3),"W:",adropset(3)
write(10,*)"Flowrate:",aflowrate(4),"m3/hr, C1/C2:",ac1c2(4),"W:",adropset(4)
write(10,*)"Flowrate:",aflowrate(5),"m3/hr, C1/C2:",ac1c2(5),"W:",adropset(5)
write(10,*)"Flowrate:",aflowrate(6),"m3/hr, C1/C2:",ac1c2(6),"W:",adropset(6)
write(10,*)"Flowrate:",aflowrate(7),"m3/hr, C1/C2:",ac1c2(7),"W:",adropset(7)
write(10,*)"Flowrate:",aflowrate(8),"m3/hr, C1/C2:",ac1c2(8),"W:",adropset(8)
write(10,*)"Flowrate:",aflowrate(9),"m3/hr, C1/C2:",ac1c2(9),"W:",adropset(9)
write(10,*)"Flowrate:",aflowrate(10),"m3/hr, C1/C2:",ac1c2(10),"W:",adropset(10)
write(10,*)
write(10,*)"-----------------------------------------------------------"


If I put it like that, it doesn't appear in the text file at all. Why is that? Do I have to change the formatting at the beggining?
 
I don't see in your code if you opened the file or not.
 
I did open, exactly the same statements as you gave me, using:

! open file
open (10, file='Final Output.txt', status='unknown')

! write to file

and...


! close file
close(10)
 
Works with your compiler the short hello world program I posted above?

 
Yes it works. And also all my code works but WITHOUT THE BIT I SHOWED ABOVE. When I put that bit, everything else appears in the text file but not that. What's wrong with it? Have I missed something?
 
I have 2 ideas:
1. It could be problem with the file name with space. Try to change 'Final Output.txt' to ''Final_Output.txt'

2. Try to write the lines to the file and to the screen too, so:
Code:
write(*,*)"Flowrate:",aflowrate(1),"m3/hr, C1/C2:",ac1c2(1),"W:",adropset(1)
write(10,*)"Flowrate:",aflowrate(1),"m3/hr, C1/C2:",ac1c2(1),"W:",adropset(1)
...
What happens?
If the info doesn't appear on the screen, then probably the code block will be executed only under a condition which never fullfil.

If nothing helps, it would be the best when you post the code here. If the program is big, reduce it to the small part which contains the problem and could be compiled and post it here.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top