使用C++實(shí)現(xiàn)監(jiān)控文件是否被修改
軟件開(kāi)發(fā)過(guò)程中經(jīng)常會(huì)用到配置文件,某些應(yīng)用場(chǎng)景要求在軟件運(yùn)行時(shí)動(dòng)態(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_H
FileWatcher.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}; // 獲取絕對(duì)路徑. _fullpath(filePath, "config.txt", 1024); std::cout <<filePath << std::endl; FileWatcher fw(filePath, 1000); system("pause"); return 0; }
這里config.txt和可執(zhí)行文件同一目錄,每次修改config.txt,就會(huì)打印File changed
到此這篇關(guān)于使用C++實(shí)現(xiàn)監(jiān)控文件是否被修改的文章就介紹到這了,更多相關(guān)C++監(jiān)控文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++實(shí)現(xiàn)strcmp字符串比較的深入探討
本篇文章是對(duì)使用C++實(shí)現(xiàn)strcmp字符串比較進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05一文帶你認(rèn)識(shí)C語(yǔ)言的聯(lián)合體和枚舉
聯(lián)合體(Union)是一種特殊的數(shù)據(jù)結(jié)構(gòu),允許在同一內(nèi)存地址上存儲(chǔ)不同類型的數(shù)據(jù),這篇文章主要給大家介紹了關(guān)于C語(yǔ)言聯(lián)合體和枚舉的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-10-10C++實(shí)現(xiàn)LeetCode(73.矩陣賦零)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(73.矩陣賦零),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07淺析C語(yǔ)言中對(duì)于char*和char[]的理解
char * s 只是一個(gè)保存字符串首地址的指針變量,char a[]是許多連續(xù)的內(nèi)存單元,單元中的元素是char型,char * 和 char a[]具有相同的效果,源于字符串的本質(zhì),這篇文章主要介紹了C語(yǔ)言中對(duì)于char*和char[]的理解,需要的朋友可以參考下2023-02-02C++中數(shù)組作為函數(shù)參數(shù)傳入的幾種方式代碼示例
數(shù)組元素和數(shù)組名都可以作為函數(shù)的參數(shù)以實(shí)現(xiàn)函數(shù)間數(shù)據(jù)的傳遞和共享,下面這篇文章主要給大家介紹了關(guān)于C++中數(shù)組作為函數(shù)參數(shù)傳入的幾種方式,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-06-06C 語(yǔ)言基礎(chǔ)教程(我的C之旅開(kāi)始了)[十]
C 語(yǔ)言基礎(chǔ)教程(我的C之旅開(kāi)始了)[十]...2007-02-02