使用C++實(shí)現(xiàn)監(jiān)控文件是否被修改
軟件開發(fā)過程中經(jīng)常會用到配置文件,某些應(yīng)用場景要求在軟件運(yùn)行時(shí)動態(tài)修改配置文件,此時(shí)就需要監(jiān)控配置文件是否被修改,如果修改了,重新加載。
FileWatcher.h
#ifndef FILEWATCHER_H
#define FILEWATCHER_H
#include <ctime>
#include <chrono>
#include <string>
#include <thread>
#include <atomic>
typedef std::chrono::system_clock Clock;
typedef std::chrono::duration<long long, std::micro> Duration;
typedef std::chrono::time_point<Clock, Duration> Time;
template <typename FromDuration>
inline Time time_cast (std::chrono::time_point<Clock, FromDuration> const & tp)
{
return std::chrono::time_point_cast<Duration, Clock> (tp);
}
inline Time now ()
{
return time_cast(Clock::now ());
}
inline Time from_time_t (time_t t_time)
{
return time_cast(Clock::from_time_t (t_time));
}
struct FileInfo
{
Time mtime;
off_t size;
};
class FileWatcher
{
public:
FileWatcher(const std::string& file, unsigned int millis);
~FileWatcher();
protected:
void run();
private:
bool checkForFileModification();
void updateLastModInfo();
bool getFileInfo(FileInfo *fi, const std::string &name);
private:
FileInfo m_lastFileInfo;
std::string m_file;
unsigned int const m_waitMillis;
std::atomic_bool m_stopped;
std::thread m_thread;
};
#endif // FILEWATCHER_HFileWatcher.cpp
#include "FileWatcher.h"
#include "tchar.h"
#include "iostream"
FileWatcher::FileWatcher(const std::string& file, unsigned int millis)
: m_file(file)
, m_waitMillis(millis)
, m_stopped(false)
, m_thread(&FileWatcher::run, this)
{
m_lastFileInfo.mtime = time_cast(Clock::now ());
m_lastFileInfo.size = 0;
updateLastModInfo();
}
FileWatcher::~FileWatcher()
{
m_stopped = true;
if (m_thread.joinable())
{
m_thread.join();
}
}
void FileWatcher::run()
{
while (!m_stopped)
{
bool modified = checkForFileModification();
if(modified)
{
updateLastModInfo();
std::cout << "File changed" << std::endl;
}
std::this_thread::sleep_for(std::chrono::milliseconds(m_waitMillis));
}
}
bool FileWatcher::checkForFileModification()
{
FileInfo fi;
if (!getFileInfo(&fi, m_file) )
{
return false;
}
bool modified = fi.mtime > m_lastFileInfo.mtime
|| fi.size != m_lastFileInfo.size;
return modified;
}
void FileWatcher::updateLastModInfo()
{
FileInfo fi;
if (getFileInfo(&fi, m_file))
{
m_lastFileInfo = fi;
}
}
bool FileWatcher::getFileInfo(FileInfo *fi, const std::string &name)
{
struct _stat fileStatus;
if (_stat(name.c_str (), &fileStatus) == -1)
{
return false;
}
fi->mtime = from_time_t(fileStatus.st_mtime);
fi->size = fileStatus.st_size;
return true;
}main.cpp
#include "FileWatcher.h"
#include <iostream>
int main(int argc, char *argv[])
{
char filePath[1024] = {0};
// 獲取絕對路徑.
_fullpath(filePath, "config.txt", 1024);
std::cout <<filePath << std::endl;
FileWatcher fw(filePath, 1000);
system("pause");
return 0;
}這里config.txt和可執(zhí)行文件同一目錄,每次修改config.txt,就會打印File changed

到此這篇關(guān)于使用C++實(shí)現(xiàn)監(jiān)控文件是否被修改的文章就介紹到這了,更多相關(guān)C++監(jiān)控文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++實(shí)現(xiàn)strcmp字符串比較的深入探討
本篇文章是對使用C++實(shí)現(xiàn)strcmp字符串比較進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
C++實(shí)現(xiàn)LeetCode(73.矩陣賦零)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(73.矩陣賦零),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
C++中數(shù)組作為函數(shù)參數(shù)傳入的幾種方式代碼示例
數(shù)組元素和數(shù)組名都可以作為函數(shù)的參數(shù)以實(shí)現(xiàn)函數(shù)間數(shù)據(jù)的傳遞和共享,下面這篇文章主要給大家介紹了關(guān)于C++中數(shù)組作為函數(shù)參數(shù)傳入的幾種方式,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-06-06

