Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Fortran: Writes to screen, not to file

  1. Apr 10, 2012 #1
    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...
  2. jcsd
  3. Apr 10, 2012 #2


    User Avatar

    Staff: Mentor

    Are you using an open() statement to open the file and "attach" it to unit 2? If so, what does it look like?
  4. Apr 10, 2012 #3
    A few questions/suggestions

    How are you launching the executable?
    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.
  5. Apr 10, 2012 #4
    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.


    I run from terminal.
  6. Apr 11, 2012 #5
    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.

  7. Apr 11, 2012 #6
    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:

    Code (Text):

    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.
  8. Apr 12, 2012 #7
    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?
  9. Apr 12, 2012 #8
    my previous post, second paragraph
  10. Apr 12, 2012 #9
    Flush is working. Awesome command, thank you!
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook