title: How to read data from ASCII STL file as a character array and convert it to real numbers?
body: <p>I'm writing a Fortran program that reads information from an ASCII STL file and writes it into another file.</p>
  
  <p>The structure of the file is as follows:</p>
  
  <pre><code>solid model
      facet normal n1 n2 n3
          outer loop
              vertex v1x v1y v1z
              vertex v2x v2y v2z
              vertex v3x v3y v3z
          endloop
      endfacet
      facet normal n1' n2' n3'
          outer loop
              vertex v1x' v1y' v1z'
              vertex v2x' v2y' v2z'
              vertex v3x' v3y' v3z'
          endloop
      endfacet
      .
      .repeats 
      .
      endsolid
  </code></pre>
  
  <p>In other words, the files consists of an unknown number of triangular facets, each containing the information in the pattern shown above.</p>
  
  <p>I'm using character allocatable character arrays to get around having to specify the format of the input file, since it varies and I have no control over it.</p>
  
  <p>This is necessary because I have no way of knowing in advance the precise format of each file being read, since they're not always the same.</p>
  
  <p>Also, in this file format, I have no way of knowing the number of facets that each file contains in advance.</p>
  
  <p>So far, the strategy I used to read the files was declaring an array of characters as</p>
  
  <pre><code>   character(len=:), dimension(:), allocatable :: line
  </code></pre>
  
  <p>and allocating and deallocating the number of positions I need available on each line, since they vary.</p>
  
  <p>If I understand it correctly, my problem is that I'm unable to allocate the positions of the array 'line' without specifying the length of each position. But I don't in fact know the length I need to allocate each position in advance. For example, in this file I have this facet: </p>
  
  <pre><code>facet normal 0.000000e+00 -1.000000e+00 0.000000e+00
     outer loop
        vertex 0.000000e+00 0.000000e+00 0.000000e+00
        vertex 1.500000e+02 0.000000e+00 0.000000e+00
        vertex 0.000000e+00 0.000000e+00 5.000000e+01
     endloop
  endfacet
  </code></pre>
  
  <p>while in another I have (Note that in this file 'outer loop' is written in the next line beginning only 1 space after 'facet normal'; in the first file, there were 3 spaces):  </p>
  
  <pre><code>facet normal -0.962637 0.19148 0.19148
   outer loop
    vertex 1.96194 0 1.30866
    vertex 2.03806 0.191342 1.5
    vertex 1.9971 0.191342 1.29409
   endloop
  endfacet
  </code></pre>
  
  <p>*) How could I read any file format using character arrays? Is it even possible?</p>
  
  <p>2) For example, if I try</p>
  
  <pre><code>  allocate(character(len=*) :: line(5))
  </code></pre>
  
  <p>I get the error:</p>
  
  <pre><code> allocate(character(len=*) :: line(5))
  
   internal compiler error: in gfc_trans_allocate, at fortran/trans-stmt.c:5697
  
   1face.f90:27:0: internal compiler error: Abort trap: 6
   gfortran: internal compiler error: Abort trap: 6 (program f951)
   Please submit a full bug report,
   with preprocessed source if appropriate.
   See &lt;http://gcc.gnu.org/bugs.html&gt; for instructions.
  </code></pre>
  
  <p>3) If I try</p>
  
  <pre><code> allocate(character(len=:) :: line(5))
  </code></pre>
  
  <p>I get </p>
  
  <pre><code> allocate(character(len=:) :: line(5))
           1
   Error: Type-spec at (1) cannot contain a deferred type parameter 
  </code></pre>
  
  <p>4) I've tried using real formats for the reading, but it doesn't work.
  If I try using an real allocatable variable and allocate it </p>
  
  <pre><code> real, dimension(:), allocatable :: line
   allocate(line(5))
  </code></pre>
  
  <p>my compiler doesn't complain, but this variable reads junk from the file: For the same example above, I get</p>
  
  <pre><code>4.15787011E+21   1.24145785E+28   2.73757760E+20   4.00465633E-11   4.00463551E-11
  </code></pre>
  
  <p>5) Finally, I've tried reading the information as a character array as </p>
  
  <pre><code>read(unit=iunit, iostat=ios) line
  </code></pre>
  
  <p>which works fine, but after the reading, I need to perform calculations with the data. So I'm thinking I could convert it to a real one-dimension array, correct? </p>
  
  <p>I've tried reading the line using the character allocatable array</p>
  
  <pre><code>read(iunit, iostat=ios) line
  </code></pre>
  
  <p>and writing its contents in a real allocatable array (3 dimension real_line) using</p>
  
  <pre><code>write(line,*) real_line
  </code></pre>
  
  <p>but then I get</p>
  
  <pre><code>Fortran runtime error: End of record
  </code></pre>
  
  <p>With that in mind, could someone help me? What should I do? Should I:</p>
  
  <ul>
  <li><p>Stick with the character reading and try to convert the data to real numbers in another way?</p></li>
  <li><p>Try something else?</p></li>
  </ul>