title: Creating a thread which writes into a file
body: <p>I am learning threads and trying to implement a code which creates a thread. The thread writes into a file. If the thread has been created it <code>returns 0</code> . The code here <code>returns 0</code> but it does go into the function <code>write()</code> but does not writes in the file . Just to check it goes in the function i have put a <code>printf()</code> statement.I want the input should be taken by command line here but it also does not work so to make it simpler  i have written only "hello world" to the file  . </p>
  
  <p>Here is the code :-</p>
  
  <pre><code>#include&lt;stdio.h&gt; 
  #include&lt;stdlib.h&gt;
  #include&lt;pthread.h&gt;
  
  void *write(void *arg)
  {
  
      printf("HI \n");
      FILE *fp;
      fp = fopen("file.txt", "a");
      if (fp == NULL) {
          printf("error\n");
      } else {
          fprintf(fp, "hello world");
      }
  }
  
  int main()
  {
      pthread_t thread;
      int tid;
      tid = pthread_create(&amp;thread, NULL, write, NULL);
      printf("thread1 return %d \n", tid);
      exit(0);
  }
  </code></pre>