Program compiled with Gfortran does not write to output file on macOS Monterey

Hello guys,
I am running a .f90 code. I want output in some file. but after compiling and running, only the output file is created. There is no data in that but the same code is showing data in the terminal window. I tried with > file.out too, but still the same issue.
This is happening after updating Monterey. I reinstall GCC and gfortran as well.

It appears that you are writing to either standard out or standard error. If so either your open statement is using the unit number for one of those, or your open statement does not have a filename, or your write statements are not using the unit number in your open statement. Please show some code showing the open and write statements.

Welcome to the forum. Have you checked that a toy program such as

program main
implicit none
open (unit=20,file="temp.txt",action="write",status="replace") ! connect unit 20 to temp.txt
write (20,*) "abc" ! write to temp.txt
end program main

writes output to the specified file? Can you post your code?

Have the same problem here after upgrading to Monterey. A workaround for me is piping standard output through cat. So a.out | cat >file.out instead of a.out >file.out.

I run your code too but only empty get the output file.
Here is my code whose printing on the terminal is fine but write command is not working

program main
implicit real(a-h,o-z)
open (unit=20,file=“temp.txt”,action=“write”,status=“replace”) ! connect unit 20 to temp.txt
do i=1,10
print*, “hello”
write (20,*) “abc” ! write to temp.txt
end do
end program main

is there any permanent solution so we can work as the previous way ??

I think you are hitting this issue: 102992 – fortran: redirecting standard out to a file does not work on macOS 12.0, the workaround for the time being is to set the environment variable GFORTRAN_UNBUFFERED_ALL=1.


We recently got a user report on one of our projects on this, here is the thread in case any of you is interested:

yes, I go with the thread. Same issue. In the meantime, Should we wait for the update or any solution?

Let’s keep the discussion here on discourse :wink:

I tried with ./a.out | cat > file.out
It worked.

Is it fine to work with the previous version of macOS? I think it is fine with it.

This post is very unclear to me !
Forget about " | cat >file.out", what happens when the executable runs ?
What output is not being produced ?

Is temp.txt being produced ? > if not, could imply gfortran is not providing data in output file.
Is file.out being produced ? > if not, could imply MacOS is not providing data in output file.

See, if I am running the following code

program main
implicit real(a-h,o-z)
open (unit=20,file=“temp.txt”,action=“write”,status=“replace”) ! connect unit 20 to temp.txt
do i=1,10
write (20,*) “abc” ! write to temp.txt
end do
end program main

The output “abc” should be written in the “temp.txt” file. Right?
But this is not happening. This code creates the “temp.txt” file but this file remains empty.

If I run this code print instead of write, it shows output in the terminal as per expectation.

program main
implicit real(a-h,o-z)
open (unit=20,file=“temp.txt”,action=“write”,status=“replace”) ! connect unit 20 to temp.txt
do i=1,10
print*, “abc” ! write to temp.txt
end do
end program main

it means, the output file is producing but the output is not showing in that output file.

Thanks for the update.
Definately looks like a gfortran implementation problem.
Would flush(unit=20) or close(unit=20) have any effect ?

Using flush(unit=20) on its own does not work. However, when using close(unit=20) it does work ok, and the file contains the output expect. Without close(unit=20) a zero byte (empty file) is created only.

! Example program to write text to a file
! Build with:  gfortran -static-libgcc -o write_file write_file.f90

program write_file
    implicit none
    open (unit=20,file="temp.txt",action="write",status="replace") ! connect unit 20 to temp.txt
    write (20,*) "abc" ! write to temp.txt
    close(unit=20)
end program write_file
3 Likes

no effect.
The same code is working with Linux(ubuntu and fedora) and windows but not with macOS Monterey.

Fixed in the latest update of gcc on Homebrew.

2 Likes

@mhulsen what version of gfortran works for you in Monterey? 11.2?