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. 云存储性能测试工具--COSBench安装

    COSBench安装 Cosbench是Intel的开源云存储性能测试软件,COSBench目前已经广泛使用与云存储测试,并作为云存储的基准测试工具使用 1 环境 1.1 操作系统 COSBench可 ...

  2. springmvc常用注解与类型转换

    springmvc常用注解与类型转换 一:前置 spring -servlet.xml 注入 <!-- 启用spring mvc 注解 --> <context:annotation ...

  3. Mac后台开发MNMP(nginx , mysql, php)标配

    mysql安装: 方法:1.原始方法,下载压缩文件,解压,安装,配置            2.dmp文件安装            3.brew安装 这里使用brew安装:      a.brew ...

  4. SQL Server 之AdventureWorks 2008 安

    学习背景:<SQL Server 2008 编程入门经典> SQL Sever 版本 SQL Server 2008 R2 方法一: 1:AdventureWorks 2008 下载地址: ...

  5. JStorm第一个程序WordCount详解

    一.Strom基本知识(回顾) 1,首先明确Storm各个组件的作用,包括Nimbus,Supervisor,Spout,Bolt,Task,Worker,Tuple nimbus是整个storm任务 ...

  6. ghost系统到硬盘完后,重启进入winxp安装的画面变成了蓝屏

    ghost系统到硬盘完后,重启进入winxp安装的画面变成了蓝屏 原因分析: 一.系统集成的驱动和主板不兼容  这种原因引起的故障在部份品牌机上较常见,主要症状是系统能正常安装完成,但完成后重启进入系 ...

  7. [华清远见]FPGA公益培训

    本套视频教程为华清远见 网络公益培训活动,主讲人:姚远老师,华清远见高级讲师. ------------------------------------------------------------ ...

  8. JsonCpp简单使用

    作者:ilife JsonCpp简单使用 1.相关概念总结 (1)解析json的方法 Json::Value json;     //表示一个json格式的对象 Json::Reader reader ...

  9. 记一次未解决的异常:java.lang.NoClassDefFoundError: net/sf/json/JSONObject

    原因:Jetty会导致这个问题,Tomcat可以正常启动   一.异常产生现象 使用json-lib转换实体类/字符串,跑单元测试没问题,但是启动jetty后调用JSONArray.fromObjec ...

  10. JQuery中ajax请求写法

    $.ajax({ type: "POST", url: "ygdwController.do?getonygdw", data : "id=" ...