utime函数:对一个文件的访问和修改时间

#include <utime.h>
int utime( const char *pathname, const struct utimbuf *times );
返回值:若成功则返回0,若出错则返回-1

此函数所使用的数据结构是:

struct utimbuf {
time_t actime; /* access time */
time_t modtime; /* modification time */
}

程序实例:

#include "apue.h"
#include <fcntl.h>
#include <utime.h>

int main(int argc,char *argv[] )

{
  int i,fd;
  struct stat statbuf;
  struct utimbuf timebuf;

  for (i=1;i<argc;i++)
  {
    if (stat(argv[i],&statbuf)<0)
    {
    /*取得当前时间*/
    err_ret("%s:stat error",argv[i]);
    continue;
    }
    if ((fd=open(argv[i],O_RDWR|O_TRUNC))<0)
    {
    /*截短*/
    err_ret("%s:open error ",argv[i]);
    continue;
  }
  close(fd);
  timebuf.actime=statbuf.st_atime;
  timebuf.modtime=statbuf.st_mtime;
  if(utime(argv[i],&timebuf)<0)
  {
  /*retset time*/
  err_ret("%s: utime error",argv[i]);
  continue;
  }
  }
  exit(0);
}

运行结果:

utime函数的更多相关文章

  1. 程序4-6 utime函数实例

    //http://blog.chinaunix.net/uid-24549279-id-71355.html /* ========================================== ...

  2. 文件和目录之utime函数

    一个文件的访问和修改时间可以用utime函数更改. #include <utime.h> int utime( const char *pathname, const struct uti ...

  3. UNIX环境编程学习笔记(11)——文件I/O之文件时间以及 utime 函数

    lienhua342014-09-16 1 文件的时间 每个文件都有三个时间字段,如表 1 所示. 表 1: 文件的三个时间字段 说明 字段 st_atime 文件数据的最后访问时间 st_mtime ...

  4. 十四、文件和目录——文件时间和utime函数

    14.1 文件时间 存在于 stat 结构体中 14.2 文件时间函数 14.2.1 介绍 utime(修改文件的存取时间和更改时间) 相关函数 utimes,stat #include <sy ...

  5. linux之utime函数解析

    [lingyun@localhost utime]$ ls hello  utime.c  world [lingyun@localhost utime]$ cat utime.c  /******* ...

  6. python第六天 函数 python标准库实例大全

    今天学习第一模块的最后一课课程--函数: python的第一个函数: 1 def func1(): 2 print('第一个函数') 3 return 0 4 func1() 1 同时返回多种类型时, ...

  7. [APUE]文件和目录(中)

    一.link.unlink.remove和rename 一个文件可以有多个目录项指向其i节点.使用link函数可以创建一个指向现存文件连接 #include <unistd.h> int ...

  8. Python标准库之核心模块学习记录

    内建函数和异常 包括__builtin__模块和exceptions模块 操作系统接口模块 包括提供文件和进程处理功能的os模块,提供平台独立的文件名处理(分拆目录名,文件名,后缀等)的os.path ...

  9. UNIX,基础知识,文件IO,文件和目录

    2015.1.27星期二,早晨阴天,中午下雪了今天上午老师不上课,程序语句,记一下:main(void){ int c; while((c = getc(stdin)) != EOF) if(putc ...

随机推荐

  1. Mono addin 学习笔记 4 再论数据扩展点(Data only extension point)

    1. Attribute声明方式 定义扩展属性 [AttributeUsage(AttributeTargets.Assembly, AllowMultiple= true)] public clas ...

  2. Clustering with the ArcGIS API for Flex

    Clustering is an excellent technique for visualizing lotss of point data. We've all seen application ...

  3. C++标准转换运算符

    C++类型转换在实际编程中会经常使用,其实,本质上对象的类型用来解释(interpret)对象.因为,每个对象都占据一块内存空间,这块内存空间存放了一段二进制数据.通过标记该对象的类型,告诉如何看待这 ...

  4. #error作用

    指令 用途 # 空指令,无任何效果 #include 包含一个源代码文件 #define 定义宏 #undef 取消已定义的宏 #if 如果给定条件为真,则编译下面代码 #ifdef 如果宏已经定义, ...

  5. WEB服务器、应用程序服务器、HTTP服务器区别

    很清晰的解释了WEB服务器.应用程序服务器.HTTP服务器区别 转载自 http://www.cnblogs.com/zhaoyl/archive/2012/10/10/2718575.html WE ...

  6. LIS检验系统,简介及主要特点

    简介 主要实现实验室设备的联机管理和信息传输以及发布,其联机共享范围小到单机版,大到医院之间或区域互联,都可以任意选择,按需升级.   主要特点 打报告不用输入姓名,报告处理轻松.高效.无差错 检查项 ...

  7. Python学习笔记(四)字符串型

    字符串是 Python 中最常用的数据类型.我们可以使用引号('或")来创建字符串. 在最新的Python 3版本中,字符串是以Unicode编码的,也就是说,Python的字符串支持多语言 ...

  8. CSS3 display:flex和display:box有什么区别

    父级元素有display:box;属性之后.他的子元素里面加上box-flex属性.可以让子元素按照父元素的宽度进行一定比例的分占空间. 如: html: <article>   < ...

  9. 控件的相对位置与绝对位置-UI界面编辑器(SkinStudio)教程

    绝对位置: 相对位置: 相对位置配合Anchor属性使用 例如Anchor属性选择left|top(即相对位置的left和top保持不变) 窗口大小改变前: 窗口大小改变后: 可以看到控件相对位置的l ...

  10. JavaScript中的Map

    1.首先,在新版本的浏览器中,已经实现了对Map的原生支持:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glob ...