fopen()有时会成功打开文本文件,并在特定时间后失败 [英] fopen() opens the text file successfully for sometime and fails after a specific time

查看:210
本文介绍了fopen()有时会成功打开文本文件,并在特定时间后失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在每10ms后在套接字上发送一个文本文件。代码工作正常,并在10ms的间隔后继续通过套接字发送文本文件。但经过一段时间(比如3-4分钟后),fopen()fils(虽然fopen()工作正常一段时间)我得到一个错误Client2.exe中0x011f28f7处的未处理异常:0xC00000FD:堆栈溢出。 并休息一下



test dword ptr [eax],eax;探测页面。

chkstk.asm。



这可能是什么原因? fopen()如何正常工作并在之后失败?



请帮助我:(



CODE :



 LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
char timer [1000];
开关(msg)
{案例WM_TIMER:
开关(wParam)
{
案例IDT_TIMER1:
{
FILE * fpSend;
if((fpSend = fopen(Client4.txt,r + b))== NULL)
{
MessageBox(NULL,
Unable打开文件,
错误!,
MB_ICONEXCLAMATION |
MB_OK);
退出(EXIT_FAILURE);
}
char file_buffer [100000 ];
fseek(fpSend,0,SEEK_END);
size_t file_size = ftell(fpSend);
fseek(f pSend,0,SEEK_SET);
if(file_size> 0)//如果文件大小> 0
{
int bytes_read = 0;
if((bytes_read = fread(file_buffer,file_size,1,fpSend))< = 0)
{
//无法将文件复制到缓冲区,
}
//文件在缓冲区中复制,

if(sendto(socketIdentifier,file_buffer,file_size,0,(struct sockaddr *)& AH_glb_connectedSocket,sizeof(AH_glb_connectedSocket))< 0 )
{
//未发送
}
其他
{
//文件已成功发送!,
sendCount = sendcount个+ 1;
memset(file_buffer,0,sizeof(file_buffer));
}

}
休息;
默认值:
返回0;
}
休息;
}
}

int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
//窗口创建。

while(GetMessage(& Msg,NULL,0,0)> 0)
{
TranslateMessage(& Msg);
DispatchMessage(& Msg);
}
返回Msg.wParam;


closesocket(socketIdentifier);
WSACleanup();

返回0;
}

解决方案

你没有打扰关闭文件,是吗?

调用

 fclose(fpSend); 

当你完成它之前(从... 的WndProc )。


I am trying to send a text file at a socket after every 10ms. The code works fine and keeps on sending the text file over the socket after an interval of 10ms. But after some period of time (like after 3-4 minutes), fopen() fils (though fopen() works fine for some duration) and I get an error "Unhandled exception at 0x011f28f7 in Client2.exe: 0xC00000FD: Stack overflow." and a break at

test dword ptr [eax],eax ; probe page.
in "chkstk.asm".

what could be the possible reason for this? How fopen() works fine for sometime and fails afterwards?

Please help me :(

CODE:

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
   char timer[1000];
    switch(msg)
      {      case WM_TIMER: 
            switch(wParam) 
               { 
                  case IDT_TIMER1: 
                   {        
                    FILE *fpSend ;
                    if((fpSend = fopen("Client4.txt", "r+b")) == NULL)
                    {
                  MessageBox( NULL,
                     "Unable to open the File",
                     "Error!",
                     MB_ICONEXCLAMATION | 
                     MB_OK);
              exit(EXIT_FAILURE);
                    }
   char file_buffer[100000];
   fseek(fpSend, 0, SEEK_END);
   size_t file_size = ftell(fpSend);
   fseek(fpSend, 0, SEEK_SET);
   if(file_size>0)   //if file size>0
      {
         int bytes_read=0;
         if((bytes_read=fread(file_buffer, file_size, 1, fpSend))<=0)
            {
            //"Unable to copy file into buffer",
            }
            //"File copied in Buffer",

         if(sendto(socketIdentifier, file_buffer, file_size, 0, (struct sockaddr *) &AH_glb_connectedSocket, sizeof(AH_glb_connectedSocket))<0)
            {
                //"Not Sent"
            }
         else
            {
                //"File Sent Successfully!",
                sendCount = sendCount+1;
                memset(file_buffer, 0, sizeof(file_buffer));
            }

               }
          break;
              default:
                 return 0;
               }
     break;
    }  
}

int WINAPI WinMain( HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
   //window created.

   while(GetMessage(&Msg, NULL, 0, 0) > 0)
      {         
         TranslateMessage(&Msg);
         DispatchMessage(&Msg);
      }
   return Msg.wParam;


   closesocket(socketIdentifier);
   WSACleanup();

   return 0;
}

解决方案

You didn''t bother to close the file, did you?
Call

fclose(fpSend);

when you''ve done with it (before returning from the WndProc).


这篇关于fopen()有时会成功打开文本文件,并在特定时间后失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆