00001__文件操作

00001__文件操作

一、简介

在编程的时候,有时候免不了要操作文件,例如覆盖或者重新编译之类的。假如要被操作的文件已经是最新的了,那就没有必要做接下来的动作了。

二、代码如下:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>

void exit_if_need_not_update(const char* in, const char* out) {
  struct stat st_in;
  struct stat st_out;

  memset(&st_in, 0x00, sizeof(st_in));
  memset(&st_out, 0x00, sizeof(st_out));

  if (in == NULL || out == NULL) {
    printf("param error!")
    exit(-1);
  }

  if (stat(in, &st_in) < 0) {
    printf("%s not exist\n", in);
    exit(-1);
  }

  if (stat(out, &st_out) < 0) {
    printf("%s not exist\n", out);
    return;
  }

  if (st_in.st_mtime < st_out.st_mtime || st_in.st_atime < st_out.st_mtime) {
    printf("Skip because: %s is newer than %s\n", out, in);
    exit(0);
  }
}

猜你喜欢

转载自blog.csdn.net/tang935892307/article/details/83926990