首页 > 其他分享 >使用inotify向文件添加一段注释

使用inotify向文件添加一段注释

时间:2022-12-01 17:04:03浏览次数:38  
标签:inotify int len 注释 添加 path include event


一 需求简介:

在ubuntu系统上任意打开一个文件(vim,touch获取其它方式),该新建的文件都会被写入一段注释。

二 实现:

编译环境:ubuntu 18.04

实现方法:利用inotify监控目录这个特性,当在一个目录里新建文件时,可以read事件,从而获取文件名,然后调用c++ ofstream写入注释。

运行效果:

使用inotify向文件添加一段注释_c++ 

实现代码:

#include <stdio.h>  
#include <string.h>
#include <stdlib.h>
#include <sys/inotify.h>
#include <unistd.h>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
static const int FILE_IN_CREATE_MASK = 256;
static const char *COMMIT_STR = "/* \n\
*@auth:hello world\n \
*@file:hello world\n \
*@license:hello world\n \
*@change story:hello world\n \
*------------------------------------\n\
*/";
const char *monitor_path = nullptr;
inline void write_commit(struct inotify_event *event) {
if (FILE_IN_CREATE_MASK == event->mask) {
if (event->len > 0 && !(event->mask & IN_ISDIR)) {
char path[128] = "";
const char *filename = event->name;
snprintf(path, sizeof(path), "%s%s", monitor_path, filename);
ofstream ofs(path);
if (!ofs) {
cerr << path << " can not be open...!" << endl;
return;
}
ofs << COMMIT_STR << endl;
ofs.close();
}
}
}
int main(int argc, char **argv) {
char buf[BUFSIZ] = "";
int len = 0;
int nread = 0;
struct inotify_event *event = nullptr;
if (argc < 2) {
fprintf(stderr, "%s path\n", argv[0]);
return -1;

}
int fd = inotify_init();
if (fd < 0) {
fprintf(stderr, "inotify_init failed\n");
return -1;
}
monitor_path = argv[1];
int wd = inotify_add_watch(fd, monitor_path, IN_CREATE);
if (wd < 0) {
fprintf(stderr, "inotify_add_watch %s failed\n", argv[1]);
return -1;
}
int n = sizeof(buf) - 1;
while ((len = read(fd, buf, n)) > 0) {
nread = 0;
while (len > 0) {
event = (struct inotify_event *)&buf[nread];
write_commit(event);
nread = nread + sizeof(struct inotify_event) + event->len;
len = len - sizeof(struct inotify_event) - event->len;
}
}

return 0;
}

编译脚本:

g++ -std=c++17 -g -o Test main.cpp

注:不支持递归目录

标签:inotify,int,len,注释,添加,path,include,event
From: https://blog.51cto.com/u_15899033/5902966

相关文章

  • 前端基础——CSS(如何查找标签、如何添加样式)
    前端基础——CSS(如何查找标签、如何添加样式)一、CSS样式表/*主要用来调节html标签的各种样式思考:页面都是由HTML构成的并且页面上有很多相同的HTML标签但是相同的H......
  • 如何通过Java代码给Word文档添加水印?
    Word中可以为文档添加的水印分为两种形式:文字水印和图片水印。水印是一种数字保护的手段,在文档上添加水印可以传达有用信息,或者在不影响正文文字显示效果的同时,为打印文档增......
  • PS新手教程-如何使用PS给照片添加浪漫的光晕效果
    如何使用PS给照片添加浪漫的光晕效果?给大家介绍如何使用PS给照片添加浪漫的光晕效果,一起来看看吧。1.打开PS,导入素材照片,按Ctrl+J快捷键复制背景图层,得到“图层1”。......
  • 032shell数组用法及多行注释
    一、Shell中将分隔符的字符串转为数组的常用方法[root@host~]#str="ONE,TWO,THREE,FOUR"[root@host~]#arr=(`echo$str|tr','''`)[root@host~]#echo${arr......
  • 手动添加定时类手动测试
    启动类加@EnableScheduling注释packagecom.runshi.cloud.zhengxie.job;importcom.runshi.cloud.common.security.annotation.Inner;importlombok.RequiredArgsCo......
  • qtablewidget中添加按钮并信号槽
    需要在cell中增加按钮。主要代码(没写类名):voidInsertTableInfo(constQStringList&listInfo,boolbChecked){intnRowCount=ui->tableWidget->rowCount();......
  • Linux添加磁盘及如何使用
    一、系统添加磁盘步骤及命令服务器添加磁盘一共分为一下四步,我们将按以下步骤详细介绍如何在服务器上添加磁盘并使用。①添加硬件②磁盘分区③格式化磁盘④挂载磁盘1......
  • Teamcenter AWC6.2 添加分类管理模块支持
    1.安装AWC基本模块,配置搜索ConfiguretraditionalbasicclassificationIfyouhaveatraditionalbasicclasshierarchyanddatainrichclient,youcane......
  • ESXI系统盘制作及安装和添加虚拟机操作步骤
    一、先将VMware-VMvisor-Installer-7.0U2a-17867351.x86_64.iso做成系统盘1、双击打开UltraISO.exe  2、点击文件—>打开,找到EXSI镜像文件,鼠标选中,点击打开。  3......
  • Spring Boot中添加Thymeleaf模板
    SpringBoot中添加Thymeleaf模板前面我们讲解了SpringBoot项目的创建、SpringBoot结构信息,自动配置功能等,那么Springboot创建出来,我们最终是要做web开发的,所以我们这章讲......