title: Fortran95 -- Reading from a formatted text file
body: <p>I need to read some values from a table.  These are the first five rows, to give you some idea of what it should look like:</p>
  
  <pre><code>1        +           3          98           96        1
  2        +         337        2799         2463        1
  3        +        2801        3733          933        1
  4        +        3734        5020         1287        1
  5        +        5234        5530          297        1
  </code></pre>
  
  <p>My interest is in the first four columns of each row.  I need to read these into arrays.  I used the following code:</p>
  
  <pre><code>program ----
  implicit none
  
  integer, parameter :: totbases = 4639675, totgenes = 4395
  integer :: codtot, ks
  integer, dimension(totgenes) :: ngene, lend, rend
  character :: genome*4639675, sign*4
  
  open(1,file='e_coli_g_info')
  open(2,file='e_coli_g_str')
  
  do ks = 1, totgenes
   read(1,100) ngene(ks),sign(ks:ks),lend(ks), rend(ks)
  end do
  100 format(1x,i4,8x,a1, 2(5x,i7), 22x)
  
  
  
  do ks = 1, 100
   write(*,*) ngene(ks), sign(ks:ks),lend(ks), rend(ks)
  end do
  
  end program
  </code></pre>
  
  <p>The loop at the end of the program is to print the first hundred entries to test that they are being read correctly.  The problem is that I am getting this garbage (the fourth row is the problem):</p>
  
  <pre><code>1 +           3   757934891
  2 +         337   724249387
  3 +        2801   757803819
  4 +        3734   757803819
  5 +        5234   757935405
  </code></pre>
  
  <p>Clearly, the fourth column is way off.  In fact, I cannot find these values anywhere in the file that I am reading from.  I am using the gfortran compiler for Ubuntu 12.04.  I would greatly appreciate if somebody would point me in the right direction.  I'm sure it's likely that I'm missing something very obvious because I'm new at Fortran.</p>