title: “Debug Assertion Failed. Expression: (_osfile(fh) & fopen)” error and Fatal Error in visual studio 2008
body: <p>My following code is compiled successfully and runs perfectly for sometime handling 40 clients but after sometime, it gives an error "Debug Assertiion Failed expression: (_osfile(fh) &amp; fopen)".</p>
  
  <p>I searched on internet and found a solution:</p>
  
  <blockquote>
    <p>Project + Properties, Configuration properties, C/C++, Code generation, change Runtime library to "Multi-threaded DLL (/MD)" (or use /MT).</p>
  </blockquote>
  
  <p>Now the Debug Assertion Failed error is not shown but the error now is "Fatal error at line fwrite()". I have Highlighted this line in code for reference.</p>
  
  <p>Can anyone explain me please why does this happen? What mistake have I made? I am stuck :(</p>
  
  <p>Sorry for the long code.</p>
  
  <p><strong>CODE:</strong></p>
  
  <pre><code>   DWORD WINAPI ThreadProc(LPVOID param)
          {
             int threadNumber= (int)param;
             int PORT = 8888+threadNumber; //so that each thread bind()s its socket to a different Port number.
             WSADATA wsa; 
  
             //Initialise winsock//
             if (WSAStartup(MAKEWORD(2,2),&amp;wsa) != 0)
                {
  
                  //"WinSock Initialization FAILED",
  
                }
  
             //Create a socket//
  
            SOCKET newSocketIdentifier;
            SOCKADDR_IN newSocket;
  
            if((newSocketIdentifier = socket(AF_INET , SOCK_DGRAM , 0 )) == INVALID_SOCKET)
                {                 
  
                  //Socket Creation Failed
  
                }
             //Socket Created//
  
             //Prepare the sockaddr_in structure//
            newSocket.sin_family = AF_INET;
            newSocket.sin_addr.s_addr = INADDR_ANY;
            newSocket.sin_port = htons(PORT);
  
             //Bind//
             if( bind(newSocketIdentifier ,(struct sockaddr *)&amp;newSocket, sizeof(SOCKADDR_IN)) == SOCKET_ERROR)
                { 
                 //Bind Failed
                }
  
             //Bind Done//   char threadNumberBuffer[32] = "Thread Number : ";
     char threadNumberBuffer[32] = "Thread Number : ";
     char buff[12];
     itoa(threadNumber,buff,10);
     strcat(threadNumberBuffer,buff);
     /*MessageBox( NULL,
                 threadNumberBuffer,
                 "Thread Created :)",
                 MB_ICONINFORMATION);*/
  
  
  char data[256];
  int bytes, waitRet;
  
  WSAEVENT hEvent = WSACreateEvent();
  WSANETWORKEVENTS events;
  WSAEventSelect(newSocketIdentifier, hEvent, FD_READ | FD_WRITE);
  
  SOCKADDR_IN clientSocket;
  int clientSocketLength = sizeof(SOCKADDR_IN);
  char receiveBuffer[8192]={0};
  int recv_len=0;
  char file[128]="Client";
  char threadno[32];
  itoa(threadNumber,threadno,10);
  strcat(threadno,".txt");
  strcat(file,threadno);
  //FILE *fprecv;
  FILE *fprecv = fopen(TEXT(file),"w+b");
  rewind(fprecv);
  int receiveCount=0;
  
     while(1)
        {   //while(1) starts
           waitRet = WSAWaitForMultipleEvents(1, &amp;hEvent, FALSE, WSA_INFINITE, FALSE);
           //WSAResetEvent(hEvent);
           if(WSAEnumNetworkEvents(newSocketIdentifier,hEvent,&amp;events) == SOCKET_ERROR)
              {
                 MessageBox( NULL,
                             threadNumberBuffer,
                             "FAILURE",
                             MB_ICONINFORMATION);
              }
          else
              {   //else event occurred starts
                 if(events.lNetworkEvents &amp; FD_READ)
                    {   //check for network event starts
                       /*MessageBox( NULL,
                                   buf,
                                   "FD_READ",
                                   MB_ICONINFORMATION);*/
  
                       if((recv_len = recvfrom(newSocketIdentifier, receiveBuffer, sizeof(receiveBuffer), 0, (struct sockaddr *) &amp;clientSocket, &amp;clientSocketLength)) == SOCKET_ERROR)
                          {
                             MessageBox( NULL,
                                         "ERROR",
                                         "Could not Receive Data",
                                         MB_ICONINFORMATION);
                             exit(EXIT_FAILURE);
                             return FALSE;
                          }
                       receiveCount = receiveCount+1;
                       char display[2000] = "Number of Receives = ";
  
                      if(memcmp(receiveBuffer,"EXIT",4) == 0)
                          {
                              char receiveCountBuffer[1000];
                              itoa(receiveCount,receiveCountBuffer,10);
                              strcat(display,receiveCountBuffer);
                              MessageBox( NULL,
                                          display,
                                          threadNumberBuffer,
                                          MB_ICONINFORMATION);
                              break;
                              //return FALSE;
                          }
                  /*MessageBox( NULL,
                              receiveBuffer,
                              "File Read",
                              MB_ICONINFORMATION);*/
                      else
                      {
                            //fprecv= freopen(TEXT(file),"w+b",stdout);
                             **if(fwrite(receiveBuffer, 1, recv_len, fprecv)&lt;0)** //Fatal Error here
                                {
                                   MessageBox( NULL,
                                               "problem while writing file",
                                               "Error!",
                                               MB_ICONINFORMATION);
  
                                   exit(1);
                                }
                             //fclose(fprecv);
                      }
                            /* MessageBox( NULL,
                                         "Written",
                                         "Success",
                                         MB_ICONINFORMATION);*/
                             rewind(fprecv);
                 }   //check for network event ends
              }   //else event occurred ends
        }   //while(1) ends
  
      WSACloseEvent(hEvent);
      return 0;
  }
  </code></pre>