Invalid memory reference when opening a file that already exists - fortran

I have some code that writes data to some files, I want to loop over this code to continue opening and writing to the files with different parameters. However, whenever I try this, once the files are created (after the first iteration of the loop) an error message occurs:
Program received signal SIGSEGV: Segmentation fault - invalid memory reference.
The code would look a little like this:
program main
! premable here
DO a=1,10
call something(a, b, c)
END DO
contains
SUBROUTINE something(a, b, c)
!premable + data manipulation
open(12, FILE = 'file.name', STATUS='UNKNOWN') ! the problem occurs here for
! the second iteration of the loop
! in the main
DO i=.... ! general loop for extracting values from array
write(12, '(4F16.12)') b(:, i)
END DO
close(12)
END SUBROUTINE something
end program main
I have tried using STATUS='OLD' rather than 'UNKNOWN' with empty files pre-created but this hasn't worked either (in fact with this the loop won't even complete the first iteration).
I am using GNU Fortran compiler with Windows and CODE::BLOCKS.

Related

HDF5 file created in Fortran not written until simulation ends

I am writing an application in Fortran that dumps a single file containing several arrays at each time step. At the end of the time-step, a new file is opened and data is written to it. Each of these files is created by calling:
! Create a new file
CALL h5fcreate_f(trim(filename), H5F_ACC_TRUNC_F, file_id, error)
and closed by calling the following subroutine close_hdf() at the end of each time step:
subroutine close_hdf()
! Close the file
CALL h5fclose_f(file_id, error)
! Close FORTRAN interface
CALL h5close_f(error)
end subroutine close_hdf
Yet, at the end of the time-step, the file is zero bytes and I have to either kill the executable or wait till all the time steps have finished
to be able to analyze the data. Why are the files not being flushed?
I have also tried to flush the data by calling
CALL h5fflush_f(file_id, H5F_SCOPE_GLOBAL , error)
inside close_hdf() but that did not help either. How do I flush and close/write these files at the end of the time step as soon as I am done with them?

Program received signal SIGSEGV while reading a file

I want to read a file named pvt.txt and just print it to the screen. But when the program runs it always showed "Program received signal SIGSEGV: Segmentation fault - invalid memory reference."
It's a fortran 90 code, just a simple code but always showed error like that. I can write a file, but I can't read it in the program. I just write a simple code to try, but it still doesn't work.
the file pvt.txt is located in the same folder with the program.
program reservoir_sim
implicit none
integer a, b, c
open(unit=10, file='pvt.txt')
read(10,*) a, b, c
print*, a, b, c
close(10)
end program reservoir_sim
In the pvt.txt file's just wrote "1, 2, 3", but it still doesn't work

How to determine file size in Fortran 77

I have a Fortran program that needs to read ASCII files, however the list of files sometimes includes a file of size 0. The program then crashes when trying to read this file. I have not find any way so far that will allow me to flag such a file.
I have following READ statement in my code
read(10,220,END=320,ERR=195)parm(1:)
although I expect code to go to statement 195, or to statement 320, without crashing, it crashes
this is where the code crashes when the file size is zero, with the following messages
...
fmt: end of file
apparent state: unit 10 named junko.con
last format: (A)
lately reading sequential formatted external IO
I tried using the INQUIRE statement
inquire (unit=10,SIZE=nsize), but the program would not compile
the OPEN statement did not give any error when opening the zero size file, and the values of IOSTAT was the same, irrespective of the file size
As Ian noted, any modern Fortran compiler should have INQUIRE. A simple test of
program foo
integer sz
inquire(file='tmp.dat',size=sz)
print *, sz
end program foo
with an empty tmp.dat file sets sz=0.

Error in fortran, undefined reference to subroutine

I am writing a subroutine and main function to call it, but getting error as undefined reference to ___. I found one reason: When I save the main and subroutine in the same file, compile and run that file, everything runs perfectly. However, when I save them into different .f90 files and try to run the main file, I get error. Is there any way I can make subroutine into a separate file and call into main calling program?
I got confused with another place - in the main program at !------ERROR------ place. I referred to Automatic width integer descriptor in fortran 90 I can use I0 as automatic width display indicator. But when I used the same, there is run time error expected integer but got character. Any idea about this?
! saved as sub_program.f90 file
SUBROUTINE sub_program (v1,v2,ctr)
IMPLICIT NONE
INTEGER, INTENT(IN) :: ctr
INTEGER, INTENT (OUT) :: v1,v2
SELECT CASE (ctr)
CASE (1)
v1=1
v2=0
CASE (2)
v1=0
v2=1
END SELECT
RETURN
END SUBROUTINE
! main calling program, saved as caller.f90
PROGRAM caller
IMPLICIT NONE
INTEGER :: v1,v2,ctr
DO ctr = 1,2,1
CALL sub_program (v1,v2,ctr)
WRITE (*,100) 'STEP = ',ctr,'V1 = ',v1,'V2 = ',v2 !------ERROR------
100 FORMAT (I0)
END DO
END PROGRAM
Thanks!
What is your compile command? For me, this compiles and runs normally
gfortran caller.f90 foo.f90 && ./a.out
I0 is an integer indicator, but some items following your WRITE statement are character strings. You can try, for example,
100 FORMAT (3(A, I0, 1X))
where 1X refers to a space.
As a note, if formatting is not terribly important and you're only interested in seeing some quick results, you can use the free format output (WRITE(*,*) ...).
EDIT: I had incorrectly referred to FORMAT as obsolete.

How to execute a command on the first and only first time entering fortran subroutine?

I wrote a subroutine for a Fortran program and I want to execute a command (delete file in the program directory and open a new one) the first, and only first time, the subroutine is called. I know that I can achieve this by creating some flag outside of the subroutine, in the main program, that I set to false at program startup and then set to true upon entering the subroutine. Then I could use this flag in an if statement to figure if the commands I want to execute on the initial call should be executed or not. But this requires me modifying the existing program and I didn't want to do that if I could avoid it. Is there some other way to do what I want to do?
An example might be:
subroutine test(a)
implicit none
integer, intent(inout) :: a
logical, save :: first_time=.true.
if(first_time) then
first_time=.false.
a = a + 12345
else
a = a - 67890
end if
end subroutine test
How about using some characteristic of the output file to determine whether or not to delete it? Time stamp, file lock, a particular file extension, etc.