title: Writing to file in Visual Studio C
body: <p>So after trying out the solutions I implemented them and I get output as "Exit".</p>
  
  <p>I am trying to write to some file, preferably csv file. Bu any file I write to, throws an error message, attached.I do not know what is going wrong.</p>
  
  <p>This is my code in Visual Studio 2015 C</p>
  
  <pre><code>#define TRANSFER_LENGTH_FROM_HOST (128)                 //Burst input
  #define TRANSFER_LENGTH_FROM_FPGA (128)                // Burst output
  #define NUM_TRANSFERS (50)                
  
  #include &lt;windows.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include "ZestSC2.h"
  
  void ErrorHandler(const char *Function, ZESTSC2_HANDLE Handle, ZESTSC2_STATUS Status, const char *Msg)
  {
         printf("**** Example2 - Function %s returned an error\n \"%s\"\n\n", Function, Msg);
         exit(1);
  }
  
  int main(int argc, char **argv)
  {
      unsigned long Count;
      unsigned long NumCards;
      unsigned long CardIDs[256];
      unsigned long SerialNumbers[256];
      ZESTSC2_FPGA_TYPE FPGATypes[256];
      ZESTSC2_HANDLE Handle;
      unsigned __int64 Freq;
      unsigned __int64 Start;
      unsigned __int64 End;
      unsigned long *Buffer_tx;
      unsigned long *Buffer_rx;
  
      FILE *fp;
      errno_t err = fopen_s(&amp;fp, "C:/Users/Shankhadeep/Documents/results.csv", "w+");
  
      // Install an error handler
      ZestSC2RegisterErrorHandler(ErrorHandler);
  
      // Request information about the system
      ZestSC2CountCards(&amp;NumCards, CardIDs, SerialNumbers, FPGATypes);
  
      printf("%d available cards in the system\n\n\n", NumCards);
  
      if (NumCards==0)
      {
          printf("No cards in the system\n");
          exit(1);
      }
  
      for (Count=0; Count&lt;NumCards; Count++)
          printf("%d : CardID = 0x%08lx, SerialNum = 0x%08lx, FPGAType = %d \n \n", Count, CardIDs[Count], SerialNumbers[Count], FPGATypes[Count]);
  
      // Open the first card
      ZestSC2OpenCard(CardIDs[0], &amp;Handle);
  
      printf("Reading the bit file .. \n");
  
      if (FPGATypes[0] == ZESTSC2_XC3S2000)
          ZestSC2ConfigureFromFile(Handle, "C:/Users/Shankhadeep/Documents/Example2_4000.bit");
      else
          ZestSC2ConfigureFromFile(Handle, "C:/Users/Shankhadeep/Documents/example2.bit");
  
      // Send data to the card - Generate a 64-bit random number in Buffer
      Buffer_tx = malloc(TRANSFER_LENGTH_FROM_HOST);
      Buffer_rx = malloc(TRANSFER_LENGTH_FROM_FPGA);
  
      for (Count = 1; Count &lt; TRANSFER_LENGTH_FROM_HOST / 2; Count++) {
          ((unsigned long *)Buffer_tx)[Count] = (unsigned long)(1 &lt;&lt; (Count - 1));
          printf("Writing %x \n", Buffer_tx[Count]);
      }
  
      QueryPerformanceFrequency((LARGE_INTEGER *)&amp;Freq);
      QueryPerformanceCounter((LARGE_INTEGER *)&amp;Start);
     for (Count=0; Count&lt;NUM_TRANSFERS; Count++)
     {
          ZestSC2WriteData(Handle, Buffer_tx, TRANSFER_LENGTH_FROM_HOST);
          for (Count = 0;Count&lt;TRANSFER_LENGTH_FROM_HOST / 2; Count++)
              //fwrite(Buffer_tx, 64, 128, fp);
              //Throws an error as soon as I write this line
              fprintf(fp, "%s", Buffer_tx[Count]);
      }
      QueryPerformanceCounter((LARGE_INTEGER *)&amp;End);
      printf("Transfer rate to card = %.2fMBytes/sec\n", ((double)NUM_TRANSFERS*(double)TRANSFER_LENGTH_FROM_HOST*(double)Freq)/(1024.*1024.*(double)(End-Start)));
  
      // Read data from the card
      QueryPerformanceCounter((LARGE_INTEGER *)&amp;Start);
     // for (Count=0; Count&lt;NUM_TRANSFERS; Count++)
     // {
          ZestSC2ReadData(Handle, Buffer_rx, TRANSFER_LENGTH_FROM_FPGA);
          for (Count = 0; Count &lt; 64; Count++) {
              printf("Reading %x \n", Buffer_rx[Count]);
  
          }
    //  }
      QueryPerformanceCounter((LARGE_INTEGER *)&amp;End); 
      printf("\n");
      printf("Transfer rate from card = %.2fMBytes/sec\n", 
          ((double)NUM_TRANSFERS*(double)TRANSFER_LENGTH_FROM_FPGA*(double)Freq)/(1024.*1024.*(double)(End-Start)));
      printf("End : %d Start: %d \n", End, Start);
  
      if (!fp)perror("fopen_s");
  printf("Error is %d", err);
  
  if (err != NULL)
      fprintf(fp, "Hello");
  else printf("Exit \n");
      ZestSC2CloseCard(Handle);
      return 0;
  }
  </code></pre>
  
  <p>The error I am getting is</p>
  
  <pre><code>Debug Assertion Failed!
  Program .. fpga_2_host.exe 
  File minkernel\crts\ucrt\src\appcrt\stdio\output.cpp Line 31
  Expression: stream != nullptr
  </code></pre>