`
runfeel
  • 浏览: 905596 次
文章分类
社区版块
存档分类
最新评论

检查一个路径下文件是否存在,如果不存在设置一个定时器,在定时器内每隔一定时间检查一次,直到该文件存在返回成功,或者定时超时返回失败

 
阅读更多

题目:

检查一个路径下文件是否存在,如果不存在设置一个定时器,在定时器内每隔一定时间检查一次,直到该文件存在返回成功,或者定时超时返回失败。



定义头文件,声明WaitForFileExists类。
#ifndef WAIT_FOR_FILE_EXISTS_H
#define WAIT_FOR_FILE_EXISTS_H

#include <iostream>
#include <string>
#include <fstream>
#include <windows.h>
#include <time.h>

using namespace std;

class WaitForFileExists
{
private:
string _fileName;
int _timeout;
string _timeoutUnit;
int _recheckTime;
string _recheckTimeUnit;

public:
WaitForFileExists(string _fileName, int _timeout, string _timeoutUnit, int _recheckTime, string _recheckTimeUnit);

bool doCheck();
bool checkFileExist();
bool convertTimeToSec(string timeUnit, int timer, int& retTimer);

};

#endif


定义WaitForFileExists类的具体实现。

#include "WaitForFileExists.h"

WaitForFileExists::WaitForFileExists(string fileName, int timeout, string timeoutUnit, int recheckTime, string recheckTimeUnit)
{
_fileName = fileName;
_timeout = timeout;
_timeoutUnit = timeoutUnit;
_recheckTime = recheckTime;
_recheckTimeUnit = recheckTimeUnit;
}

bool WaitForFileExists::doCheck()
{
if (_fileName == "" || _timeout <= 0 || _timeoutUnit == "" || _recheckTime <= 0 || _recheckTimeUnit == "")
{
cout << "The input parameter is wrong!" << endl;
return false;
}
if(checkFileExist() == true)
{
return true;
}
else
{
cout<<"The file path is not exist, we will check it again until timeout."<<endl;
double dif = 0;
time_t start,end;
time(&start);
int timer;
if(false == convertTimeToSec(_timeoutUnit, _timeout, timer))
{
cout<<"Error>>> the parameter is wrong, just support s, m, h."<<endl;
return false;
}
int recheckTimer;
if(false == convertTimeToSec(_recheckTimeUnit, _recheckTime, recheckTimer))
{
cout<<"Error>>> the parameter is wrong, just support s, m, h."<<endl;
return false;
}
while(dif < (double)timer)
{
time(&end);
dif = difftime(end,start);
Sleep(recheckTimer - 1);
if(checkFileExist())
{
return true;
}
}
return false;
}
}

bool WaitForFileExists::checkFileExist()
{
std::ifstream fin(_fileName.c_str());
if(fin.is_open())
{
fin.close();
return true;
}
else
{
return false;
}
}

bool WaitForFileExists::convertTimeToSec(string timeUnit, int timer, int& retTimer)
{
if(timeUnit == "m")
{
retTimer = timer*60;
}
else if(timeUnit == "s")
{
retTimer = timer;
}
else if(timeUnit == "h")
{
retTimer = timer*3600;
}
else
{
cout<<"Error>>> the parameter is wrong, just support s, m, h."<<endl;
return false;
}
return true;
}


main函数中检验该类的实现的正确性。
#include "WaitForFileExists.h"

int main()
{
WaitForFileExists isFileExists("d:/fuqiang.txt", 5, "m", 10, "s");
if(isFileExists.doCheck())
{
cout<<"find the file"<<endl;
}
else
{
cout<<"Fail to find the file."<<endl;
}

getchar();
}



The MIT license:

Permission is hereby granted, freeof charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnishedto do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR INCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics