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
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.
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
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