title: How to pass a constant string from fortran function to C
body: <p>I'm having troubles to pass a string from Fortran to  C. The C code passes a string that contains a filepath  to a Fortran function. The function reads in the file and should return a two-characters string.</p>
  
  <p>Here is my C code</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;math.h&gt;
  #include &lt;sys/time.h&gt;
  #include &lt;iostream&gt;
  #include &lt;iomanip&gt;
  #include &lt;fstream&gt;
  #include &lt;sstream&gt;
  #include &lt;time.h&gt;
  #include &lt;ctype.h&gt;
  using namespace std;
  
  extern "C" void read_id_type_(char *file_path, char *id_type, size_t *path_len_file);
  
  int main(int argc, char **argv) {
  
  char path[500],id_type[2];
  char *dir_path, *file_path;
  dir_path=path;
  size_t path_len=strlen(dir_path);
  
  file_path=strcat(path,"/rnspar_mpt1.dat");
  size_t path_len_file=strlen(file_path);
  
  read_id_type_(file_path,id_type,&amp;path_len_file);
   printf("id_type is %s\n",id_type);
  
   return EXIT_SUCCESS ;
   }
  </code></pre>
  
  <p>Here is my Fortran function</p>
  
  <pre><code> integer function read_id_type(filename,id_type,path_len) 
   implicit none
  
   integer :: nrg, nrf, nrf_deform, nrgin,path_len
   character(400) :: filename
   character(2) ::  id_type,EQ_point
  
   filename=filename(1:path_len)
   write(*,*),"Reading file", filename
  
   open(unit = 1,file = trim(filename), status='old')
   read(1,'(4i5)') nrg
   read(1,'(4i5)') nrf
   read(1,'(2i5,2(3x,a2))') nrf_deform, nrgin, id_type, EQ_point
   close(1)
   print *,"Id_type=",id_type
   read_id_type = 0
   end function read_id_type
  </code></pre>
  
  <p>The output is:</p>
  
  <pre><code>Id_type=IR    (From the Fortran side)
  
  id_type is IRV2?      (From the C side)
  </code></pre>
  
  <p>The output, in the C code, should be only the first two characters. 
  Any help would be much appreciated</p>