title: How to write to file in C
body: <p>I have a program I am working on that will start and stop a servo. I can issue the following command from the command line and it works. <code>echo 2=120 &gt; /dev/servoblaster</code> that will start the servo in motion. Now I have the following program </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main(void)
  {
      FILE *fp;
  
  
     fp = fopen("/dev/servoblaster", "w");
     if (fp == NULL) {
         printf("Error opening file\n");
         exit(0); 
   }
     fprintf(fp, "2=120");
     fclose(fp);
     fflush(fp); 
     return 0;
  }
  </code></pre>
  
  <p>But when I execute it nothing happens, now when I try the <code>echo 2=120 &gt; /dev/servoblaster</code> command it will say <code>Bad input: 2=1202=120</code> but if i repeat the same <code>echo 2=120 &gt; /dev/servoblaster</code> command it will work again. If i was to try and execute my above program 3 times the output when I try to execute the <code>echo</code> command its output will be <code>Bad input 2=1202=1202=120 2=120</code> so to me it seems like the file is not finished being written to in my program. Can someone point out if I am missing something?</p>