title: Python ctypes writing data to be read by C executable
body: <p>I'm trying to learn how to use the Python ctypes library to write data to a file that can easily be read by C executables. In the little test case that I've put together, I'm running into some problems with reading/writing character arrays.</p>
  
  <p>At the moment, I have three source files. <code>write_struct.py</code> creates a simple struct with two
  entries, an integer value called <code>git</code> and a character array called <code>command</code>, then writes the struct to a file using <code>ctypes.fwrite</code>. <code>read_struct.c</code> and <code>read_struct.h</code> compile into an executable that internally defines an identical struct to the one in <code>write_struct.py</code>, then reads in the data written by the python script and prints it out.</p>
  
  <p>At the moment, the following values are assigned in the python file (not literally in the manner shown below, scroll down to see the actual code):</p>
  
  <pre><code>git = 1
  command = 'cp file1 file2'
  </code></pre>
  
  <p>And when run, the C executable prints the following:</p>
  
  <pre><code>git: 1
  command:
  </code></pre>
  
  <p>I realize that the problem is almost certainly in how the <code>command</code> variable is being assigned in the python script. I have read that <code>c_char_p()</code> (the function I'm currently using to initialize the data in that variable) does not create a pointer to mutable memory, and <code>create_string_buffer()</code> should be used instead, however I'm not sure about how this works with either adding that data to a struct, or writing it to a file. I guess I'm also confused about how writing pointers/their data to a file works in the first place. What is the best way to go about doing this?</p>
  
  <p>Thanks in advance to anyone that is able to help!!</p>
  
  <p>The code of my three files is below for reference:</p>
  
  <p><code>write_struct.py</code>:</p>
  
  <pre><code>"""
  write_struct.py
  """
  
  from ctypes import *
  
  libc = cdll.LoadLibrary("libc.so.6")
  
  class DataStruct(Structure):
      _fields_ = [("git", c_int),
                  ("command", c_char_p)
                  ]
  
  def main():
      pydata = DataStruct(1, c_char_p("cp file1 file2"))
  
      libc.fopen.argtypes = c_char_p, c_char_p
      libc.fopen.restype = c_void_p
  
      libc.fwrite = libc.fwrite
      libc.fwrite.argtypes = c_void_p, c_size_t, c_size_t, c_void_p
      libc.fwrite.restype = c_size_t
  
      libc.fclose = libc.fclose
      libc.fclose.argtypes = c_void_p,
      libc.fclose.restype = c_int
  
  
      f = libc.fopen("stored_data", "wb")
      libc.fwrite(byref(pydata), sizeof(pydata), 1, f)
      libc.fclose(f)
      return 0
  
  main()
  </code></pre>
  
  <p><code>read_struct.c</code>:</p>
  
  <pre><code>/*
   * read_struct.c
   *
   */
  
  #include "read_struct.h"
  
  int main()
  {
    data_struct cdata = malloc(DATASIZE);
    FILE *fp;
    if ((fp = fopen("stored_data", "r")) != NULL) {
      fread(cdata, DATASIZE, 1, fp);
      printf("git: %i\n", cdata-&gt;git);
      printf("command:");
      printf("%s\n", cdata-&gt;command);
      fclose(fp);
    } else {
      printf("Could not open file\n");
      exit(1);
    }
    return 0;
  }
  </code></pre>
  
  <p><code>read_struct.h</code>:</p>
  
  <pre><code>/*
   * read_struct.h
   *
   */
  
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  typedef struct _data_struct *data_struct;
  struct _data_struct {
    int git;
    char command[40];
  };
  #define DATASIZE sizeof(struct _data_struct)
  </code></pre>