title: Segmentation fault when writing to a file
body: <p>UPDATED CODE FULLY FUNCTIONAL*** </p>
  
  <p>I'm new to C/C++ and I wrote a piece of code that would count the number of times the program has run by writing 'count' to a file each time. Except I'm getting a seg fault error when I try to run it. Anyone can tell me why ?</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  
  int main(int argc, const char * argv[]) {
      FILE *fp;
      printf("LINE 13 CLEAR");
      fp = fopen("MyPlayground.rtf", "r");
      int value;
      fscanf(fp, "%d", &amp;value);
      printf("Value is %d", value);
      //printf("LINE 14 CLEAR");
      if(fp== NULL)
      {
          printf("LINE 17 CLEAR");
          if(fopen("MyPlayground.rtf", "w")==NULL)
          {
              exit(0);
          }
          //fprintf(fp,"%d",0);
          fclose(fp);
          return 0;
      }
      //printf("LINE 25 CLEAR");
      fp = fopen("MyPlayground.rtf", "w");
      //printf("LINE 30 CLEAR");
      fprintf(fp,"%d",++value);
      fclose(fp);
  }
  </code></pre>
  
  <p>EDIT: The program runs until "printf("LINE 17 CLEAR");" and after that it gives me an 'EXC-BAD-ACCESS' error in the following line.</p>
  
  <p>EDIT1: Fixed fixed the two different paths</p>
  
  <p>EDIT2: Added error check for second <code>fopen(...)</code> statement. </p>
  
  <p>Also, feel free to comment on any style errors and things I should/should not do. Thanks.</p>