Fortran: Writes to screen, not to file


by gluons
Tags: file, fortran, screen, writes
gluons
gluons is offline
#1
Apr10-12, 10:03 PM
P: 15
I've never had this issue before. My code will create the file and also output anything to terminal. But if I change the output from terminal to file, it does nothing. For example, this line

write(*,*) "ist1, ist2 =",ist1,ist2

outputs my two indices in every loop iteration. But if I add or replace it with this line,

write(2,*) "ist1, ist2 =",ist1,ist2

nothing happens. I have tried changing the filename, the unit number, what I am outputting, and where I put the write statement, and I can't figure it out.

Anybody know what I'm missing here? I've never had issues writing to file before...
Phys.Org News Partner Science news on Phys.org
Better thermal-imaging lens from waste sulfur
Hackathon team's GoogolPlex gives Siri extra powers
Bright points in Sun's atmosphere mark patterns deep in its interior
jtbell
jtbell is offline
#2
Apr10-12, 10:26 PM
Mentor
jtbell's Avatar
P: 11,231
Are you using an open() statement to open the file and "attach" it to unit 2? If so, what does it look like?
gsal
gsal is offline
#3
Apr10-12, 11:12 PM
P: 838
A few questions/suggestions

How are you launching the executable?
double-click?
from command line?

If you double-click on it, the OS may be launching a separate terminal/dos window with its own environment and default working directory, etc...so, it may be running your program in some other directory and not the one you think.

To correct this situation, open a terminal and change directory to the directory where you actually have the executable. THEN, run the program by typing its name at the prompt.

If you are in Linux, make sure to list ALL files in the directory, even the ones typically hidden (.*)

To be sure, clean up the directory as much as possible to have only the files that you need to run the program...the executable itself and possibly an input file.

Run the program.

Inspect the directory one more time and see if an additional file appeared. If you did not explicitly open unit 2 and gave it a name...fortran will create a default file name along the lines of fort2 or something like that.

gluons
gluons is offline
#4
Apr10-12, 11:52 PM
P: 15

Fortran: Writes to screen, not to file


The code creates the file just fine. I can change the name of the file if I want to. It just won't write anything to it. I get no compile or run errors either, just nothing being written to file.

open(unit=2,file="test.OUT")

I run from terminal.
Well_InTheory
Well_InTheory is offline
#5
Apr11-12, 12:57 AM
P: 14
Is your file writable? (check directory permissions)

Also, I'd add a status='unknown' to the open command to make sure it's not worried about overwriting.

Tried printing the values of ist1,ist2 to screen with a print command at the same time?

Tried watching the file real-time to make sure it doesn't get populated then wiped? (tail -f test.OUT)

Tried changing the unit number to something (other than 5 or 6 of course)? (changing the open command also)

Tried adding a meaningless print *,"this does nothing" just above the write(2,*)? I know, this sounds like a weird suggestion, but if you overflow somewhere just before this it can screw or fix things automagically - the output might be buffered.

HTH.
gsal
gsal is offline
#6
Apr11-12, 05:42 AM
P: 838
Yeah, that buffer thing is something good to keep in mind; although I doubt that's the problem right now, since it is said that the program run without errors.

But, when there are errors and the program terminates prematurely and does not write anything out, it IS because the write commands did not make it out of buffer. On those circumstances, I add a "flush" command after every set of "write" commands to force emptying the buffer and learn what is going on.

And, also...yes...the OPEN command could use more arguments; you may want to add the 'status' and say 'unknown' or 'new'; 'unknown' is the default, so, that should have work...but better to be safe.

Is your code very long? Can you post it? Or, before you do that...go ahead and do the same exercise of compiling a program, and running it the same way you are doing now...but instead, use this program:

program try
write(2,*) 'testing, testing, 1, 2, 3...'
end program try
This program works for me and it creates file "fort.2"

See what it does for you...if it works, you have a totally different problem...maybe you program is not quite running error free, maybe there is a memory mangling problem...I don't know...post it, if it is not too long.
gluons
gluons is offline
#7
Apr12-12, 12:29 PM
P: 15
So it turns out that this does in fact write to file, but only after the entire program is finished completing. How can I ask it to perform these writes individually?
gsal
gsal is offline
#8
Apr12-12, 12:43 PM
P: 838
my previous post, second paragraph
gluons
gluons is offline
#9
Apr12-12, 12:49 PM
P: 15
Quote Quote by gsal View Post
my previous post, second paragraph
Flush is working. Awesome command, thank you!


Register to reply

Related Discussions
Getting fortran to read a text file and write the whole file to a different location Programming & Computer Science 4
Fortran 95: WRITE to screen changes output to WRITE to file Programming & Computer Science 13
Turn off writing to screen in Fortran 90 Programming & Computer Science 2
Bash script - Monitor File Writes? Programming & Computer Science 9
using a batch file to create NAMELIST input file for FORTRAN Programming & Computer Science 0