分類

展開全部 | 收合全部

分類

展開全部 | 收合全部

【C++ 系統控制 #2】C++ inotify sample code 偵測指定路徑底下的文件變化 (內有範例程式碼)

基礎概念

inotify 是取用 linux 底下的系統自動監聽檔案變化的方式,
在 C++ 中,當我們獲得了系統給的 event,我們會得到該檔案的「檔名」,
我們就能再進一步依照此絕對路徑做對應的資料操作。

注意:inotify 的功能目前只在 linux 系統能使用,mac 上不能使用哦~ (自己親自測試過QQ)

使用範例

inotify 做到的效果就是:如果當某個資料夾底下有資料變化,會告訴你「檔名」
舉一些例子例如:
– 新增:IN_CREATE
– 修改:IN_MODIFY
– 刪除:IN_DELETE
– 資料被開啟:IN_OPEN
– 資料屬性變化:IN_ATTRIB (可搭配touch使用,獲得目標檔案路徑)

下方程式碼說明

這是一個範例程式碼,
用途是透過無限迴圈偵測指定資料夾路徑 “./target_path/” 底下的「不間斷的」檔案變化。

Sample Code

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/inotify.h>
#include <unistd.h>
#include <iostream>
#include <cxxabi.h>
#include <typeinfo>

#define EVENT_SIZE  (sizeof(struct inotify_event))
#define BUF_LEN     (1024 * (EVENT_SIZE + 16))

using namespace std;


void get_file_path_loop(string watch_path){

  char buffer[BUF_LEN];
  int length, i = 0;
  int fd, wd;

  /* https://man7.org/linux/man-pages/man7/inotify.7.html */
  fd = inotify_init();

  if (fd < 0) {
      perror("inotify_init");
  }

  wd = inotify_add_watch(fd, watch_path.c_str(), IN_MODIFY | IN_CREATE | IN_DELETE | IN_ATTRIB);  

  while(1){   
    length = read(fd, buffer, BUF_LEN);  
    printf("[Event] The file length = %d\n", length);

    if (length < 0) {
        perror("read");
    }

    i = 0;
    while (i < length) {
        struct inotify_event *event = (struct inotify_event *) &buffer[i];

        if (event->len) {
            if (event->mask & IN_CREATE) {
                printf("[Event IN_CREATE] The file %s/%s was created.\n", watch_path.c_str(), event->name);
            } else if (event->mask & IN_DELETE) {
                printf("[Event IN_DELETE] The file %s/%s was deleted.\n", watch_path.c_str(), event->name);
            } else if (event->mask & IN_MODIFY) {
                printf("[Event IN_MODIFY] The file %s/%s was modified.\n", watch_path.c_str(), event->name);
            } else if (event->mask & IN_ATTRIB) {
                printf("[Event IN_ATTRIB] The file %s/%s was IN_ATTRIB.\n", watch_path.c_str(), event->name);  
            }
        }
        i += EVENT_SIZE + event->len;
    }
  }


  (void) inotify_rm_watch(fd, wd);
  (void) close(fd);
}

int main(int argc, char **argv) {
    string watch_path = "./target_path/";
    get_file_path_loop(watch_path);    

    return 0;
}

Reference

⭐C++ 基礎用法 相關文章整理⭐:
1.【C++】C++ compile 程式碼 使用 c++ 11 與使用相關的 package
2.【C++】C/C++ 顯示資料的類別 (type) sample code (內含範例程式碼) print C data type, cout C++ data type, get variable type in c++
3.【C++】C++ 複製 2D array的方法 copy 2d array memcpy sample code (內含範例程式碼)
⭐Modern C++ ⭐:
⭐C++ 字串處理相關文章整理⭐:
1.【C++】字串 char string stringstream 相關用法總整理 (內含範例程式碼) 與利用 sprinf, snprinf assign 值的方法
2.【C++】字串 char string stringstream 「轉換」用法總整理 (內含範例程式碼)
3.【C】printf, fprintf, sprintf, snprintf 相關用法總整理 (內含範例程式碼)
4.【C++】C++ String 用法 連接兩個 String c++ string concat
⭐C++ 系統偵測相關文章整理⭐:
1.【C++】C++ 利用 dirent.h 計算資料夾的檔案數量 count files sample code (內含範例程式碼)
2.【C++】C++ inotify sample code 偵測指定路徑底下的文件變化 (內有範例程式碼)
⭐C++ OpenCV 相關文章整理⭐:
1.【OpenCV】c++ OpenCV – 在 ubuntu 上第一次執行 OpenCV 程式 sample code (內含範例程式碼)
2.【OpenCV】c++ OpenCV - 在圖片上寫上文字 cv::putText sample code (內含範例程式碼)
3.【OpenCV】c++ OpenCV - cv::Rect 矩形用法與相關功能函數 sample code (內含範例程式碼)
4.【OpenCV】c++ OpenCV - OpenCV 中的純量 定義顏色 cv::Scalar(255,255,255) color sample code (內含範例程式碼)
5.【OpenCV】用 C++ 計算 iou 的方法 與網路算法常見錯誤(內附範例程式碼) sample code
⭐C++ Makefile 相關文章整理⭐:
1.【C++ Makefile】- 1 / 嘗試撰寫自己的第一份 Makefile
2.【C++ Makefile】- 2 / 新增自己的變數
3.【C++ Makefile】- 3 / Makefile 常用變數 -「$@」, 「$^」
4.【C++ Makefile】- 4 / Makefile 常用 fake target -「.PHONY」
5.【C++ Makefile】- 5 / Makefile 內建變數 -「$@」, 「$^」, 「$<」, 「$* 」,「$? 」
⭐【喜歡我的文章嗎? 歡迎幫我按讚~ 讓基金會請創作者喝一杯咖啡!
如果喜歡我的文章,請幫我在下方【按五下Like】 (Google, Facebook 免註冊),會由 「LikeCoin」 贊助作者鼓勵繼續創作,讀者們「只需幫忙按讚,完全不用出錢」哦!

likecoin-steps
Howard Weng
Howard Weng

我是 Howard Weng,很多人叫我嗡嗡。這個網站放了我的各種筆記。希望這些筆記也能順便幫助到有需要的人們!如果文章有幫助到你的話,歡迎幫我點讚哦!
另外,因為定位是「個人的隨手筆記」,有些文章內容「⚠️可能我理解有誤⚠️」或「?只寫到一半?」,如果有發現這樣的情況,歡迎在該文章的最下面留言提醒我!我會儘快修正或補上!感謝大家的建議與幫忙,讓網站能變得更好?

文章: 890

★留個言吧!內容有誤或想要補充也歡迎與我討論!