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

题目:

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



定义头文件,声明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 copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies 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.



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值