首页 > 编程语言 >mock touch with c/c++

mock touch with c/c++

时间:2022-12-01 17:04:54浏览次数:38  
标签:return c++ ofs cerr file touch path include mock


verson 0:

#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <utime.h>
#include <iostream>
#include <fstream>
using namespace std;
const char *file_path = nullptr;
inline bool need_to_create_file() {
if (0 == access(file_path, R_OK)) {
return false;
}
ofstream ofs(file_path, ios::in | ios::app);
if (!ofs) {
cerr << file_path << " create failed...!" << endl;
}
else {
cout << file_path << " create ok." << endl;
ofs.close();
}
return true;
}
inline void update_file() {
utimbuf utim_buf = {time(NULL), time(NULL)};
if (utime(file_path, &utim_buf) < 0) {
cerr << strerror(errno) << endl;
}
else {
cout << "update " << file_path << " access time ok." << endl;
}
}
int main(int argc, const char **argv) {
if (argc != 2) {
cerr << "please input file path." << endl;
return -1;
}
file_path = argv[1];
if (true == need_to_create_file()) {
return 0;
}
update_file();

return 0;
}

 

标签:return,c++,ofs,cerr,file,touch,path,include,mock
From: https://blog.51cto.com/u_15899033/5902964

相关文章

  • C++编程技巧: Pimpl
    Pimpl(Pointertoimplementation)是一种减少代码依赖和编译时间的C++编程技巧,其基本思想是将一个外部可见类(visibleclass)的实现细节(一般是所有私有的非虚成员)放在一个......
  • 《安富莱嵌入式周报》第293期:SEGGER开源其C/C++库源码emRun,丰富EMC电磁兼容资,OTA开源
    往期周报汇总地址:http://www.armbbs.cn/forum.php?mod=forumdisplay&fid=12&filter=typeid&typeid=104 视频版:https://www.bilibili.com/video/BV1ND4y1v7ik/ 1、......
  • C++学习笔记——内联函数
    //#include<iostream>//usingnamespacestd;////#defineSUM(x)((x)*(x))//定义一个宏参数//////inlinevoidfun(inti)//{//cout<<(i*......
  • C++学习笔记——operator
    //#include<iostream>//usingnamespacestd;//////classStu//{//public://inta;//doubleb;////Stu()//{//a=12;//b......
  • C++学习笔记——类内operator
    //#include<iostream>//usingnamespacestd;////classStu//{//public://inta;//Stu()//{//a=26;//}//intoperator+(intb)......
  • C++学习笔记——二元运算符
    //#include<iostream>//usingnamespacestd;////classStu//{//public://inta;//Stu(inta1)//{//a=a1;//}////关系运算符重......
  • C++学习笔记——一元运算符
    //#include<iostream>//usingnamespacestd;////classStu//{//public://inttemp;//Stu(intt)//{//temp=t;//}////负号//......
  • C++学习笔记——static累加
    //#include<iostream>//usingnamespacestd;////classStu//{//public://staticintb;//静态成员无论赋值如何变化,一个静态成员只有一个空间//......
  • 剑指offer题解C++版
    一,常见数据结构1,数组3-找出数组中重复的数字4-二维数组中的查找5-替换空格29-顺时针打印矩阵leetcode989-数组形式的整数加法leetcode26-删除有序数组中的重复......
  • 关于c++的预定义宏
    有时候看到代码里充斥着宏,很难受,这些宏都是啥玩意,哪来的。有些是库或者代码定义的,有些是编译器定义的,有些是cmake里面定义的,再在代码里使用。查看或者说查询语言标准,编译器......