C/C++ プログラムのログ ファイルへの書き込みに問題があります。問題が発生するコードの例を次に示します
EnterCriticalSection(&critical);
printf("\nWaiting for a connection on TCP port %d (nbr of current threads = %d)...\n", pServer->TCPServerPort, (*pServer->lChildInfo));
AddLog("Waiting for a connection on TCP port %d (nbr of current threads = %d)...", pServer->TCPServerPort, (*pServer->lChildInfo));
LeaveCriticalSection(&critical);
// creating variables to be passed to the thread
struct*ThreadData = (struct*) malloc(sizeof(struct));
ThreadData->csock = (int*)malloc(sizeof(int));
memcpy(&ThreadData->pServer,&pServer,sizeof(pServer));
if((*ThreadData->csock = accept( pServer->ListenSocket, (SOCKADDR*)&sadr, &addr_size))!= INVALID_SOCKET ){
ThreadData->dwIP = sadr.sin_addr.s_addr;
ThreadData->wPort = sadr.sin_port;
printf("Received connection from %s:%d \n",inet_ntoa(sadr.sin_addr), ntohs(sadr.sin_port));
AddLog("Received connection from %s:%d ",inet_ntoa(sadr.sin_addr), ntohs(sadr.sin_port));
AddLog は、ファイルに書き込むために私が書いた関数です:
FILE *fichier = NULL;
va_list ap;
va_start(ap, log);
//fichier = fopen("log.log","a");
fichier = _fsopen("log.log", "a", SH_DENYNO);
if (fichier == NULL)
printf("Error log: %d (%s)\n", errno, strerror(errno));
else {
fprintf(fichier,":");
vfprintf(fichier, log, ap);
fprintf(fichier,"\n");
va_end(ap);
fclose(fichier);
}
私が本当に説明できないのは、最初の AddLog ("Waiting for...." とそれ以前のすべてのもの..) がファイルに正しく書き込まれているということです。しかし、接続を試みると、その後のログ (... から接続を受信) がファイルに書き込まれず、常にエラー 13 "アクセスが拒否されました" が表示されます。ファイルに chmod 777 を使用しました。_fsopen 関数も試しましたが、スレッドに入るとこのエラーが発生します。誰かが何か考えを持っていれば、それは本当に役に立ちます。ありがとうございます