title: fgets() crashes my program
body: <p>I'm a newbie in C programming. I'm trying to open a file (juanjo2.klb), its a binary file I've created with the same program, that I'm going to show here. The thing is that I'm trying to use the fprintf() function to print a string to the file and then, use fgets() to write the string into the 'cad' string, and use the function printf() to print to the screen the string I've read from file.</p>
  
  <p>When I run the program, the program crashes and Windows (Windows 7) gives me a message box saying that my program stopped working and it's finding a solution to the problem. So I really don't know what I'm doing wrong becasue I think I'm using the functions correctly. I need your help. Here is the source code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;conio.h&gt;
  
  #define MAXCHARS 20
  
  int main()
  {
      char cad[MAXCHARS];
      FILE *pf;
      char *nombre = "\\juanjo2.klb";
      if(pf = fopen(nombre, "a+b") == NULL)
      {
         printf("Error opening file\n");
         exit(-1);
      }
  
      fprintf(pf, "%s", "Hola como estas");
  
      while(feof(pf) == 0)
      {
          fgets(cad, 13, pf);
          printf("%s", cad);//Here I print the string to the screen.
      }
  
      fclose(pf); //Closing file.
  
      system("PAUSE");
      return 0;
  </code></pre>
  
  <p>}</p>
  
  <p>I'm using the Codeblocks IDE 10.05. Thank's for your help.</p>