gfortran produce error when read from a txt file - fortran

I'm a beginner with Fortran and I'm trying to code a simple program in Fortran using Eclipse. The idea is to read two integer numbers from a .txt file, then write their summation on other .txt file and save it. The codes were compiled and run sucessfully in Eclipse, however when I run its executable file, it produces this error:
At line 6 of file ../Alo.f95 (unit = 11, file = 'SumData3.txt')
Fortran runtime error: End of file
Error termination. Backtrace:
#0 0x108c17579
#1 0x108c18235
#2 0x108c18999
#3 0x108ce001b
#4 0x108cdaa39
#5 0x108c10c73
#6 0x108c10e2a
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.
Here are my codes
program test_random_number
implicit none
integer :: x,y,z
open(unit=11, file="SumData3.txt",status='old')
read(11,*) x,y
close(11)
z = x+y
open(unit=12, file="SumData4.txt")
write(12,*) "Summation =", z
close(12)
end
Could you please help me?
Thank you alot.

Related

"End of file" when reading from standard input with an online Fortran compiler

I am running the following code via an online Fortran compiler.
PROGRAM ONE
IMPLICIT NONE
REAL:: v
READ (*,*) v
IF ( sqrt(v) > 1. ) THEN
WRITE(*,*) 'At first: sqrt(v) > 1.'
ELSE IF ( sqrt(v) < 1. ) THEN
WRITE(*,*) 'At first: sqrt(v) < 1. '
ELSE
WRITE(*,*) 'At first: sqrt(v) == 1.'
END IF
END PROGRAM ONE
I get the following message.
$gfortran -std=gnu *.f95 -o main
$main
At line 7 of file main.f95 (unit = 5, file = 'stdin')
Fortran runtime error: End of file
Error termination. Backtrace:
#0 0x7fb0b576beda
#1 0x7fb0b576ca85
#2 0x7fb0b576d24d
#3 0x7fb0b58e3513
#4 0x7fb0b58dc459
#5 0x7fb0b58ddbb9
#6 0x400824
#7 0x4009bd
#8 0x7fb0b4c334d9
#9 0x400719
#10 0xffffffffffffffff
When you have a read statement like
READ (*,*) v
you may typically expect the program to pause, waiting for data from standard input. However, it's possible that a normal file has been redirected to standard input, in which case the program tries to read from that file. If that file is empty, then an end-of-file condition (see your runtime error message) will occur, rather than the program waiting.
In your case, with your online compiler, there is a tab next to the source code where you can enter the data which will be used as standard input. Unless you put the input here, you will see this failure.

"Segmentation fault - invalid memory reference" possibly related to number types problem. How do I change my number types?

I am trying to use an old Fortran code for processing data. I have little experience with Fortran and have been unable to get past a problem that I think is to do with number types.
Part of the code I am using is at the bottom of this question. I am pretty sure, but not certain, that the second-last line (also the second-last line of this post) is the problem.
First I did this:
gfortran -g cpt_ir_.f90 -o cpt_ir_.o
./cpt_ir_.o < di.in
It resulted in this error:
Backtrace for this error:
#0 0x2B794A134467
#1 0x2B794A134AAE
#2 0x2B794ABC724F
#3 0x2B794A1FB8AB
#4 0x2B794A1F7613
#5 0x2B794A1F934E
#6 0x2B794A1FDF86
#7 0x40128A in MAIN__ at cpt_ir_.f90:29
Segmentation fault
I searched Stack Overflow and saw a suggestion to do the following to get more information:
gfortran -g -fcheck=all -Wall cpt_ir_.f90
The output is shown directly below. The Fmax... line is the final line of the code I pasted at the end of this post (further in the code there are other similar lines). However, I see that it is shown as a warning, not an error. So I although I proceed here as though it is the error, maybe there is another problem that the command above did not reveal.
cpt_ir_.f90:66.5:
Fmax=N*((4000.0/(2.0*Pi))*(2.0*Pi*timestep*1.0-15.0*29979245800.0))
1
Warning: Possible change of value in conversion from REAL(8) to INTEGER(4) at (1)
I came across a suggestion here at Stack Overflow to use the following flags:
-fdefault-integer-8 -fno-range-check
Which I did as follows:
gfortran -g -fdefault-integer-8 -fno-range-check cpt_ir_.f90 -o cpt_ir_.o
I'm not sure if I did it correctly. I also tried them one by one. Anyway, there was no change and I got the same error. I also tried manually changing the numbers in the problem line as shown in the final line of this post. That didn't help either--I got an error that the largest number was too large for an int.
If anyone could please point me in the right direction, I would be very grateful. Please also feel free to change my tags if there are more appropriate tags for this question.
cpt_ir_.f90:
!
!
IMPLICIT NONE
INTEGER, PARAMETER :: dp=KIND(0.0D0)
REAL(KIND=dp), DIMENSION(:), ALLOCATABLE :: correlation
REAL(KIND=dp) :: integral,omega,Pi,timestep
REAL(KIND=dp), DIMENSION(3,1000000) :: dipder,dip
REAL(KIND=dp), DIMENSION(3) :: m_vec
INTEGER :: N,I,J,Nmax,Fmax
CHARACTER(LEN=100) :: line,filename
Pi=4.0D0*ATAN(1.0D0)
READ(5,*) filename
READ(5,*) timestep
OPEN(10,FILE=filename)
N=0
DO
READ(10,'(A100)',END=999) line
IF (INDEX(line,' XXX').NE.0) THEN
N=N+1
READ(line(45:),*) dip(:,N)
ENDIF
IF (INDEX(line,' XXX').NE.0) THEN
N=N+1
READ(line(45:),*) dipder(:,N)
ENDIF
ENDDO
999 CONTINUE
CLOSE(10)
Nmax=N/10
print *, Nmax
ALLOCATE(correlation(0:Nmax))
correlation=0.0_dp
DO I=1,N-Nmax
DO J=I,I+Nmax
correlation(J-I)=correlation(J-I)+DOT_PRODUCT(dipder(:,I),dipder(:,J))
ENDDO
ENDDO
DO I=0,Nmax
correlation(I)=correlation(I)/(REAL(N-I,kind=dp)*REAL(N,kind=dp))
ENDDO
OPEN(UNIT=10,FILE="dip_dip_correlation.time")
write(10,*) "XXX"
DO I=-Nmax,Nmax
write(10,*) I*timestep,correlation(ABS(I))/correlation(0)
ENDDO
CLOSE(10)
OPEN(UNIT=10,FILE="XXX")
write(10,*) "XXX"
!Fmax up to 4000 cm^-1
Fmax=N*((4000D0/(2*Pi))*(2.0D0*Pi*timestep*1.0D-15*29979245800.0))
! My try: Fmax=N*((4000.0/(2.0*Pi))*(2.0*Pi*timestep*1.0-15.0*29979245800.0))
Update based on Dan's answer:
Dan kindly pointed out that I needed to uncomment "N=N+1." Unfortunately, after fixing that, I am still seeing the segmentation fault. Just now when I ran:
gfortran -g -fcheck=all -Wall cpt_ir_.f90
on my try at the last line of the code (where I tried converting everything to a float):
Fmax=N*((4000.0/(2.0*Pi))*(2.0*Pi*timestep*1.0-15.0*29979245800.0))
I got:
Fmax=N*((4000.0/(2.0*Pi))*(2.0*Pi*timestep*1.0-15.0*29979245800.0))
1
Warning: Possible change of value in conversion from REAL(8) to INTEGER(4) at (1)
At a glance, your line
READ(line(45:),*) dip(:,N)
is the first problem. You comment out the N=N+1 line so N = 0. Fortran is '1' indexed meaning that Fortran arrays start at 1 unless otherwise specified. So the second dimension of dip starts at 1 and you are trying to set the 'zeroth' element which does not exist.

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

Problem when reading Fortran file with CodeBlocks IDE

Recently, i've begun learning Fortran programmation language.
I am using CodeBlocks IDE with GNU Fortran Compiler.
I have a problem in simple code that i found in a Fortran Course online that explains how to read and write from a file.
The program is the following:
program main
implicit none
character (len=14) :: c1,c2,c3
integer :: n
real :: T
open(unit=10,file='titi.txt')
read(10,*) c1,n,c2
read(10,*) c3,T
close(10)
open(unit=20,file='toto.txt')
write(20,*) c1,'il est',n,c2
write(20,*)'la',c3,'est de',T,'degres'
close(20)
end
Where the file 'titi.txt' contains:
bonjour 4 heures
temperature 37.2
The error message that appears in the console is the following:
Program received signal SIGSEGV: Segmentation fault - invalid memory
reference.
Backtrace for this error:
#0 ffffffff
I tried using the flag
-g
And than i found using the debugger that the problem is in the first line where 'read' was used
read(10,*) c1,n,c2
I really don't know how to deal with this. The code seems pretty simple to me and i have never seen this error message before, so i don't know what does it mean.
Thanks for your answers in advance.
Thank you all for your responds.
Actually what caused the problem is that i was using an old compiler. So when i downloaded the last version it all worked perfectly without changing any line in the code.
This is not an answer, but it's too much text for a comment.
It's running fine on my computer.
Can you compile it with
gfortran -g -O0 -fbacktrace -Wall -fcheck=all
That way you should get a lot more information. Also, you can add some error checking:
Add the following variables:
integer :: ios
character(len=100) :: iomsg
Then you can add error checking to all io statements like this:
read(10,*) c1,n,c2
becomes:
read(10,*,iostat=ios,iomsg=iomsg) c1,n,c2
if (ios /= 0) then
print*, "Error reading c1, n, c2:"
print*, trim(iomsg)
STOP
end if
That can also give you some hints.

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