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.
Related
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.
I have a situation unexpected with my fortran program.
I want to drop some data in a text file. For some reasons, I created a type to deal with this file.
Therefore, I open a file with the commands (where f is my file type object):
open(newunit = f%unit, &
file = trim(adjustl(f%name)), &
form = 'FORMATTED', &
access = 'STREAM', &
action = 'WRITE', &
status = 'REPLACE', &
iostat = ios)
if(ios /= 0) print '("Problem creating file")', trim(f%name)
Then I write stuff like this:
write(unit=f%unit,fmt='(100A)') "#Header"
And when it is done, I want to close the file. To do this, I call the subroutine:
subroutine close_file_ascii(f)
implicit none
class(my_file_type) intent(in) :: f
logical :: file_opened, file_exist
integer :: ios
inquire(unit=f%unit, exist=file_exist, opened=file_opened) !, iostat=ios)
print *,'file_exist:', file_exist, 'file_opened:', file_opened, 'ios:', ios
if ((file_opened) .and. (file_exist)) then
close(unit=f%unit)
else
print *,"[WARNING]"
end if
end subroutine close_file_ascii
My problem is in this last subroutine. When I run the program on windows, I get the following error:
Fortran runtime error: Inquire statement identifies an internal file
Error termination. Backtrace
Therefore, I tried to create MWE to understand the problem, but all of them where working well. So couldn't really isolate the problem. Also a strange thing is that when I compile and execute with gfortran on my linux there is no problem, but when I do so on my windows I get the previous error. ( I compile on windows with gfortran version 7.3.0 x86_64-posix-sjlj-rev0, Built by MinGW-W64 )
I already work-around this problem by uncommenting the end of inquire line in the close subroutine, and everything seems to work fine. And I get the following print:
file_exist: T file_opened: T ios: 5018
But I would to understand what is going on. So what could create such internal file error (while the file should not be internal but external)? Is my workaround could be harmful ? Is it a bug ? Is there a better way to close safely an opened file? Any ideas to isolate the problem ?
EDIT
From roygvib's comment, the following test seems to replicate the problem:
program bug
implicit none
integer :: i
character(len=1) :: s
write (s,'(i1)') 0
open(newUnit=i,file='bug.txt',status='unknown')
inquire(unit=i)
end program bug
The update to gfortran 8.1 solved the problem.
I'm sorry if this has been asked before, but I couldn't find an answer that seemed to work for me. I'm working with an old program, but have made a few modifications to it.
I can include the whole 2500 line program, but it seems like that is a lot.
I've successfully compiled the a program, but it fails when I try and run it. I'm getting a "Fortran runtime error: End of file" at the line which reads the .dat file. I've tried to compile a test segment, using the same .dat file and same variables. It results in the same problem.
PROGRAM OPEN
INTEGER (KIND=1), PARAMETER :: dy=3 ! number of income states
INTEGER (KIND=2) :: OpenStatus
REAL, DIMENSION(dy) :: grid,wt
OPEN(1,file='cygdrive/user/mk.dat',status='old',form='formatted',IOSTAT=OpenStatus)
READ (1,*) grid, wt
IF(OpenStatus>0) STOP 'cannot open mk.dat'
CLOSE(1)
PRINT*, grid(1)
END PROGRAM
The data file referenced is:
-1.7320508e+000
0.0000000e+000
1.7320508e+000
4.1777138e-001
1.6710855e+000
4.1777138e-001
Where each of these numbers is on its own line and preceeded by a space
This generates the same end of file runtime error. I'd really appreciate any help here.
I should add that I compiled with gfortran.
EDIT:
As per High Performance Mark's suggestion below, I've modified it to include an inquire test.
PROGRAM TEST
CHARACTER :: fnm, seq, fort
Logical :: lex
INTEGER (KIND=1), PARAMETER :: dy=3 ! number of income states
INTEGER (KIND=2) :: j,j0,j1,j2,j4,j5,j6,j7,k,jjj,jj,dyy,OpenStatus
REAL, DIMENSION(dy) :: grid,wt
OPEN(1,file='cygdrive/user/mk.dat',status='old',form='formatted',IOSTAT=OpenStatus)
INQUIRE (1, EXIST=lex, NAME=fnm, SEQUENTIAL=seq, FORMATTED=fort)
PRINT*, 'Exists=',lex, ' Name=',fnm, ' Sequential=', seq, 'Formatted=', fort
READ (1,*) grid, wt
IF(OpenStatus>0) STOP 'cannot open mk.dat'
CLOSE(1)
PRINT*, grid(1)
END PROGRAM
The results of the inquire statement are:
Exists= T Name= Sequential=U Formatted=U
My understanding is that the File is found (i.e. exists is returned as true), is un-named and the format and sequential access are returned as unknown (as is direct which I included later). I've also checked the delimiter and padding which are coming back as unknown.
My beginner intuition is telling me that I should try and create a new data file by writing to it with a fortran program and that should solve the problem? Is that correct? If so is there a fundamental misunderstanding at play here i.e. is this a problem with data files from other sources?
Thanks for all your patience.
(Answered in the comments. See Question with no answers, but issue solved in the comments (or extended in chat) )
The OP wrote:
I've fixed this problem-the program was looking for it in a different place, I've now corrected that.
I am very new Fortran and I am doing some elementary practice. I have installed Plato latest edition. I found this program in the net, and try to compile it
program dotprod
implicit none
real :: c
real, dimension(3) :: a, b
print*,'Enter first vector'
read*, a
print*,'Enter second vector'
read*, b
c = a(1)*b(1) + a(2)*b(2) + a(3)*b(3)
print*,'Dot product = ', c
end program dotprod
Plato shows no sign of error when I build and compile, but when I try to run the program the following error message shows up: the executable does not exist.
can anyone help me explain how to handle this error ?
thanks
Maybe your file name is too long. I had the same error message, and then I saved the file using another shorter name and suddenly it worked... I also tried your code and it works on my Plato (FTN95 Personal Edition (FTN95PE) version 7.20) on windows 10.
I've recently started working on an existing Fortran program, and picking up the language at the same time. I wrote the following subroutine:
subroutine timing(yyyy, mm, dd, var, ntime, time_blocks,
* time_day)
use myglobals
! ---------------------------------------------------------------------
! Common Variables
! ---------------------------------------------------------------------
integer yyyy, ! year
* mm, ! month
* dd, ! day
* ntime ! nr of blocks for which time was measured
real time_blocks(ntime),
* time_day
character*4 var
! ---------------------------------------------------------------------
! Internal Variables
! ---------------------------------------------------------------------
integer ios
integer out_unit=52
open(unit=out_unit, file=diroutput(1:69)//'timing',
* err=450, iostat=ios)
450 print*, "iostat= ", iostat
print*, "open"
write(out_unit, format_str) yyyy, mm, dd, var, time_blocks,
* time_day
return
end
The purpose of this subroutine is to write the inputs it gets from another part of the program to a file, following a defined format (format definition not included in my example). The file must be created on the first call of this subroutine, then accessed on each further call in order to append the new information. diroutput is a character string defined in myglobals.
My problem is that the program seems to get hung up at the OPEN statement, i.e. nothing happens until I kill the process. I ran the code with several print*, statements to locate the error, and found out this way that the error must be in the OPEN statement. It seems strange that the program does nothing at all, not even jump to the error label.
As I'm new to Fortran I might be missing something fairly obvious, so a quick look by someone more experienced might help. I'm certain that diroutput contains a valid path.
I'm using Linux (CentOS 5.5) and I compiled my program with Intel Fortran Compiler 11.1.
Your code seems, from the continuation characters in (generally) column 6, to be written in fixed-form despite containing features of Fortran 90. If it is fixed-form then statement labels, such as 450 should be in columns 1 to 5. I don't immediately see why that would cause the program to hang rather than crash, but I suggest you fix this and try again.