检查一个路径下文件是否存在,如果不存在设置一个定时器,在定时器内每隔一定时间检查一次,直到该文件存在返回成功,或者定时超时返回失败
来源:互联网 发布:系统动力学软件vensim 编辑:程序博客网 时间:2024/05/22 08:15
题目:
检查一个路径下文件是否存在,如果不存在设置一个定时器,在定时器内每隔一定时间检查一次,直到该文件存在返回成功,或者定时超时返回失败。
定义头文件,声明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.
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 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS 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 IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- 检查一个路径下文件是否存在,如果不存在设置一个定时器,在定时器内每隔一定时间检查一次,直到该文件存在返回成功,或者定时超时返回失败
- 检查一个文件夹是否存在,如果不存在就创建它
- 已知ftp上的一个文件完整路径,在idftp中是否可以测试该文件是否存在!
- 判断用户是否存在不存在添加存在返回一个值
- 判断指定路径下文件是否存在
- C# 检查文件夹和文件是否存在
- C# 检查文件夹和文件是否存在
- PHP检查一个数组内是否存在指定元素in_array
- 在VC里如何检查一个文件是否存在
- 设置窗体的起始位置,并检查配置文件是否存在,存在使用配置文件,不存在创建配置文件,在每次打开窗体时保持窗体上一次关闭的位置。
- 检查数据库中一个表是否存在
- C#+WPF+oracle 检查文件服务器上文件是否存在
- 通过文件锁实现,程序开始运行时,先判断文件是否存在,若存在则表明该程序已经在运行了,如果不存在就用open函数创建该文件,程序退出时关闭文件并删除文件
- 定时检查某个目录下文件脚本
- 检查文件夹是否存在,不存在则创建
- Linux下文件及目录是否存在
- 查找当前目录下文件是否存在,不存在则弹出选择文件对话框
- 检查一个类,对象是否存在一个方法
- 如何设置cmd命令的初始目录
- 2.1 2750:鸡兔同笼
- github简单使用方法
- android api总结(2)
- C语言数据结构学习之数组线性表
- 检查一个路径下文件是否存在,如果不存在设置一个定时器,在定时器内每隔一定时间检查一次,直到该文件存在返回成功,或者定时超时返回失败
- swap VS gcd的多种实现方式
- Boost.Thread扯淡
- wince下实现FTP的功能
- 2.2 1657:棋盘上的距离
- 快速排序->快速选择算法
- java按值传递
- 2.3 2808:校门外的树
- 开始学windows内核或驱动编程注意事项