Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Fortran90 Segmentation fault

  1. Oct 18, 2012 #1
    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
     
  2. jcsd
  3. Oct 18, 2012 #2

    Mark44

    Staff: Mentor

    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.
    I believe this is your problem:
    Code (Text):

    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, 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: Oct 18, 2012
  4. Oct 18, 2012 #3

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    You can declare a Fortran array with any integer bounds if you want, for eaxmple
    Code (Text):

    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.
     
  5. Oct 18, 2012 #4
    Thanks, solved!
     
  6. Oct 18, 2012 #5

    Mark44

    Staff: Mentor

    I thought there would be a way to do it. What I meant to say was that by default, an array is 1-based.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Fortran90 Segmentation fault
  1. Segmentation Fault (Replies: 9)

Loading...