How can I change the starting index of an array in Fortran?

  • Topic: Fortran 
  • Thread starter Thread starter Giammy85
  • Start date Start date
  • Tags Tags
    Fault Fortran90
Join the discussion
Registration is free. Start your own thread to ask a follow-up.
4 replies · 5K views
Giammy85
Messages
19
Reaction score
0
I'm try to read some files in subdirectories and create some other files in my present location:

program multiread
!
implicit none

!variables
integer :: n, k, i
integer :: j, m2
real(8) :: pratio, crap, rmsttv
character(len=42) :: filein(98)
character(len=10) :: fileout(14)
character (LEN=26) :: path
character(len=2) number, mass

j = 1
do i=0, 6
m2=5*i
if (m2==0) then
m2=1
end if
write(mass,998) m2
998 format(i2.2)
write(*,*) j
fileout(i)=mass//'_000.dat'
write(*,*) fileout(i),j
open(unit=i+10,file=fileout(i),status='replace',access="append")
! write(*,*) j
do n=0,6
pratio=1.5+0.25*n
! write(*,*) pratio
write(number,998) j
! write(*,*) j
! 999 format(i2.2)
write(*,*) number
path='/scratch/project3/set1/'//number//'/'
filein(j)=path//'periodrmsttv.dat'
write(*,*) filein(j)
open(unit=j+40,file=filein(j),status='old')

read (j+40,*) crap
read (j+40, '(f14.6)') rmsttv
write (i+10,'(f4.2,f14.6)') pratio, rmsttv
close (j+40)

j=j+2
end do
close (i+10)
end do

end program multiread

I receive a segmentation fault message: debugging I have found the value of j change from 1 to a random number just after having defined fileout(i)=mass//'_000.dat'
How to solve this problem?
Thanks
 
Physics news on Phys.org
When you post a code sample, put [ code ] and [ /code ] tags around it (omit the spaces I showed). This preserves your indentation and makes your code easier to read. I have done that below.
Giammy85 said:
I'm try to read some files in subdirectories and create some other files in my present location:
Code:
program multiread
!
implicit none 

!variables
integer :: n, k, i
integer :: j, m2
real(8) :: pratio, crap, rmsttv
character(len=42) :: filein(98)
character(len=10) :: fileout(14)
character (LEN=26) :: path
character(len=2) number, mass

j = 1
do i=0, 6
m2=5*i
if (m2==0) then
m2=1
end if
	   write(mass,998) m2
	998  format(i2.2)
      write(*,*) j
	   fileout(i)=mass//'_000.dat'
      write(*,*) fileout(i),j
	   open(unit=i+10,file=fileout(i),status='replace',access="append")
 !     write(*,*) j
	do n=0,6	
	pratio=1.5+0.25*n
!      write(*,*) pratio
	      write(number,998) j
!      write(*,*) j
!	 999  format(i2.2)
      write(*,*) number
		path='/scratch/project3/set1/'//number//'/'
	      filein(j)=path//'periodrmsttv.dat'
      write(*,*) filein(j)
	      open(unit=j+40,file=filein(j),status='old')

		read (j+40,*) crap
		read (j+40, '(f14.6)') rmsttv
		write (i+10,'(f4.2,f14.6)') pratio, rmsttv
		close (j+40)

	j=j+2
	end do
close (i+10)
end do

end program multiread
I receive a segmentation fault message: debugging I have found the value of j change from 1 to a random number just after having defined fileout(i)=mass//'_000.dat'
How to solve this problem?
Thanks

I believe this is your problem:
Code:
do i=0, 6
  m2=5*i
  if (m2==0) then
  m2=1
  end if
  write(mass,998) m2
  998 format(i2.2)
  write(*,*) j
  fileout(i)=mass//'_000.dat'
  .
  .
  .
end do
Arrays in Fortran are 1-based, by default[/color], which means that the smallest index is 1. In your code, i starts out at 0, and you attempt to store the value of mass in fileout(0).
 
Last edited:
Mark44 said:
Arrays in Fortran are 1-based, which means that the smallest index is 1. In your code, i starts out at 0, and you attempt to store the value of mass in fileout(0).

You can declare a Fortran array with any integer bounds if you want, for eaxmple
Code:
character(len=10) :: fileout(0:14)
makes the smallest index 0 not 1.

But if you are going to use Fortran a lot, you need to get used to working with arrays that start from index 1.
 
AlephZero said:
You can declare a Fortran array with any integer bounds if you want, for eaxmple
Code:
character(len=10) :: fileout(0:14)
makes the smallest index 0 not 1.
I thought there would be a way to do it. What I meant to say was that by default, an array is 1-based.
AlephZero said:
But if you are going to use Fortran a lot, you need to get used to working with arrays that start from index 1.