请大家帮小弟我看下,下面的程序是哪的有关问题,为什么说打开的文件太多
请大家帮我看下,下面的程序是哪的问题,为什么说打开的文件太多
请大家帮我看下,下面的程序是哪的问题,为什么说打开的文件太多
VOS_INT32 LOG_Util_Io::copyFile(const char *szSrcPath, const char *szDstPath)
{
int iCount = 0;
const int SHAW_MAX_BUF=5242880;
char *pBuf = new char[SHAW_MAX_BUF];
//begin add by ss in 20100416
string strCmd="";
strCmd =strCmd + "cp "+ szSrcPath + " " + szDstPath;
//end add by ss in 20100416
if (VOS_PTR_NULL == pBuf)
{
return LOG_UTIL_IO_MALLOC_FAIL;
}
memset(pBuf , SHAW_MAX_BUF, 0);
ifstream isFile(szSrcPath);
ofstream osFile(szDstPath);
if (!isFile)
{
delete []pBuf;
isFile.clear();
//begin add by ss when copy failed;
if (0 == system(strCmd.c_str()))
{
return 0;
}
cout<<"copy file failed1."<<"errno:"<<errno<<endl;//这里返回的错误号errno是24,即“Too many open files”
//end add by ss on 2010-4-21
return -1;
}
if (!osFile)
{
delete []pBuf;
osFile.clear();
//begin add by ssss when copy failed;
if (0 == system(strCmd.c_str()))
{
return 0;
}
cout<<"copy file failed2."<<"errno:"<<errno<<endl;
//end add by ssssss on 2010-4-21
return -1;
}
while (isFile.read(pBuf,SHAW_MAX_BUF))
{
iCount = isFile.gcount();
osFile.write(pBuf,iCount);
}
iCount = isFile.gcount();
osFile.write(pBuf,iCount);
//¹Ø±ÕÓëÊäÈëÊä³öÎļþÁ÷Ïà¹ØÁªµÄ¾ä±ú
isFile.close();
osFile.close();
delete []pBuf;
return 0;
}
------解决方案--------------------
文件句柄在操作系统中是有限制的,看你的函数里面漏洞相当多,在每一个出口都是要调用close的。
请大家帮我看下,下面的程序是哪的问题,为什么说打开的文件太多
VOS_INT32 LOG_Util_Io::copyFile(const char *szSrcPath, const char *szDstPath)
{
int iCount = 0;
const int SHAW_MAX_BUF=5242880;
char *pBuf = new char[SHAW_MAX_BUF];
//begin add by ss in 20100416
string strCmd="";
strCmd =strCmd + "cp "+ szSrcPath + " " + szDstPath;
//end add by ss in 20100416
if (VOS_PTR_NULL == pBuf)
{
return LOG_UTIL_IO_MALLOC_FAIL;
}
memset(pBuf , SHAW_MAX_BUF, 0);
ifstream isFile(szSrcPath);
ofstream osFile(szDstPath);
if (!isFile)
{
delete []pBuf;
isFile.clear();
//begin add by ss when copy failed;
if (0 == system(strCmd.c_str()))
{
return 0;
}
cout<<"copy file failed1."<<"errno:"<<errno<<endl;//这里返回的错误号errno是24,即“Too many open files”
//end add by ss on 2010-4-21
return -1;
}
if (!osFile)
{
delete []pBuf;
osFile.clear();
//begin add by ssss when copy failed;
if (0 == system(strCmd.c_str()))
{
return 0;
}
cout<<"copy file failed2."<<"errno:"<<errno<<endl;
//end add by ssssss on 2010-4-21
return -1;
}
while (isFile.read(pBuf,SHAW_MAX_BUF))
{
iCount = isFile.gcount();
osFile.write(pBuf,iCount);
}
iCount = isFile.gcount();
osFile.write(pBuf,iCount);
//¹Ø±ÕÓëÊäÈëÊä³öÎļþÁ÷Ïà¹ØÁªµÄ¾ä±ú
isFile.close();
osFile.close();
delete []pBuf;
return 0;
}
------解决方案--------------------
文件句柄在操作系统中是有限制的,看你的函数里面漏洞相当多,在每一个出口都是要调用close的。