Program received signal SIGSEGV while reading a file - fortran

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

Related

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.

Invalid memory reference when opening a file that already exists

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.

Simple reading csv file tutorial code gives an error

CodeBlocks 17.12 for Fortran with GNU fortran compiler in Windows10
I am totally new to Fortran, and would like to read csv file with Fortran by the simple tutorial code below. However, it gives me an error: 'Program received signal SIGSEGV: Segmentation fault - invalid memory reference.' and I totally got lost.
The question might be too simple, but I really appreciate it if someone could give me an advice.
program readSimpleCSV
implicit none
integer, parameter :: n = 5
real x, y, z
integer i
open (17, file='book1.csv', status='old')
read (17, '()') ! skipping the header
do i = 1, n
read (17, *) x, y, z
print *, x, y, z
end do
close (17)
end program readSimpleCSV
The csv file: 'book1.csv' is as below
X,Y,Z
1.2,3.4,5.0
2.2,3.3,6.2
0.3,2.1,5.98
4.2,3.6,1.25
5.03,2.3,3.44

FORTRAN Read() issue

Hey so I am trying to learn Fortran basics so that I can use it for a basic physics project. I am having trouble with getting input properly. My code is:
program main
write(*, *) "Enter n:"
read(*, *) n
print *, "Number is ", n
end program main
It is quite simple. Except that when I compile and run it, nothing happens until I enter the input in which it looks like this
gfortran num.f90 -o num
./num
(nothing happens until I type) 3
Enter n:
Number is: 3
Is there a problem with my memory allocation? Or could it be my compiler? I honestly have no clue.
Your program is fine, on my machine it prints the Enter n: before reading the variable. If you do not see the message until you enter a value (and hit Enter), it could have to do with the buffering of your command window you use. For checking this, you could just open an xterminal (type xterm in your command window), and run the program there.

Fortran I/O, first read is EOF?

I'm trying to use some old FORTRAN code with some new Java code which works in Windows(as an exe) but not in OS X. I try to build it in eclipse and I get
make: *** [all] Segmentation fault: 11
so I go to terminal and do it that way, even different compilers but still the same result:
Running OS X 10.7.5 and gfortran-4.2 made
with standard -c and -o commands
Program foo
open(unit = 1, file = 'variables.txt',IOSTAT= iost)
write(*,*)iost
read(1,*) P
write(*,*)P
...
end program foo
the program builds manually but the output is:
0
At line 13 of file Cubic42.f
Fortran runtime error: End of file
I have also seen this error:
list in: end of file
apparent state: unit 88 named variables
last format: list io
lately reading sequential formatted external IO
Abort
It shows that IOSTAT returns 0, which means the file is good?
But it will not read the file, even if I change the unit# to say, 88.. and even if I change the CR to mac, windows, or unix.
It seems to be only a problem with the input/output, if i hardcode variables, for example, the program works.
Any ideas on how to fix this?
Thanks in advance.
Edit1
Here is the variables.txt file
-9999
15.6
500
150.9
48.98
0.000
there is a carriage return at the end, and it doesn't matter if i change the -9999 to positive
Edit2
I deleted the text file from the directory and remade the .f to look like this:
program foo
implicit none
real a, b, c, d
open(unit = 1, file = 'variables.2txt', action='write')
write(1, *) -6666
write(1, *) 6.15
write(1, *) -321
write(1, *) 5.16
close(1)
open(unit = 2, file = 'variables.2txt', action='read',form='FORMATTED')
read(2, *) a
write(*,*) a
read(2, *) b
write(*,*) b
read(2, *) c
write(*,*) c
read(2, *) d
write(*,*) d
close(2)
end program foo
Then I compiled it.
Output is:
-6666.000
6.150000
-321.0000
5.160000
as expected, but variables.2txt is nowhere to be found! I'm very confused, please help.
Edit3
I have found the phantom file. It is located at /users/me/phantom.txt
So the question is, how do I make the file save in the same directory as the executable?
I get a very similar error message to yours
0
At line 4 of file proba.f (unit = 1, file = 'variables.txt')
when running your code on Linux with a file variables.txt where I explicitly set the end of line characters according to the old MAC convention to ^M (instead of Unix's ^J). So, I guess, it is an EOL-convention problem. You could eventually try to write two lines to a file and investigate that file in order to decide which EOL-convention gfortran expects on your system:
program foo
implicit none
open(unit = 1, file = 'variables.txt', action='write')
write(1, *) -9999
write(1, *) 15.6
close(1)
end program foo
Also, I'd definitely go for a more recent gfortran compiler (current stable is version 4.7.2).