Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1056512
  • 博文数量: 573
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 66
  • 用 户 组: 普通用户
  • 注册时间: 2016-06-28 16:21
文章分类

全部博文(573)

文章存档

2018年(3)

2016年(48)

2015年(522)

分类: LINUX

2015-12-02 16:54:38


点击(此处)折叠或打开

  1. #include <sys/stat.h>
  2. #include <sys/types.h>
  3. #include <time.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <unistd.h>

  8. void report(struct stat *ptr);
  9. int JugeFileExist(char *lFileName);

  10. int main(int argc, char ** argv)
  11. {
  12.     if(argc < 2)
  13.     {
  14.         printf("参数个数错误,应为2个!\n");
  15.         return -1;
  16.     }

  17.     char path[128];
  18.     memset(path, 0, sizeof(path));
  19.     strcpy(path, argv[1]);
  20.     printf("path = [%s]\n", path);
  21.     struct stat ptr;
  22.     memset(&ptr, 0, sizeof(ptr));
  23.     printf("1*********************************\n");
  24.     stat(path, &ptr);
  25.     printf("st_mode = [%o]\n", ptr.st_mode);
  26.     int iRet = ptr.st_mode & S_IFREG;
  27.     printf("iRet = [%o]\n", iRet);
  28.     printf("iRet = [%d]\n", iRet);
  29.     if(iRet > 0)
  30.     {
  31.         
  32.     }
  33.     
  34.     printf("2*********************************\n");
  35.     report(&ptr);
  36.     return 0;
  37. }


  38. /***********************************************************
  39.  * 函 数: JugeFileExist()
  40.  * 功能描述: 判断文件是否存在
  41.  * 输入参数:lFilename    -    长文件名
  42.  * 返 回:0-文件存在;1-文件不存在
  43.  * 流程描述:
  44.  *    说 明:
  45.  * 修改记录:
  46.  * [修改人] [日期][描述]
  47. ***********************************************************/
  48. int JugeFileExist(char *lFileName)
  49. {
  50.     
  51.     struct stat statbuf;
  52.     memset(&statbuf,0,sizeof(statbuf));
  53.     stat(lFileName,&statbuf);
  54.     printf("st_mode = [%o]\n", statbuf.st_mode);
  55.     int iRet = statbuf.st_mode & S_IFREG;
  56.     printf("iRet = [%o]\n", iRet);
  57.     printf("iRet = [%d]\n", iRet);
  58.     if(iRet > 0)
  59.         return 0;
  60.     else
  61.         return 1;
  62.     
  63. }


  64. void report(struct stat *ptr)
  65. {
  66.     
  67.     printf("3*********************************\n");
  68.     printf("The major device no is:%d\n",major(ptr->st_dev));/*主设备号*/
  69.     printf("The minor device no is:%d\n",minor(ptr->st_dev));/*从设备号*/
  70.     printf("The file's node number is:%d\n",ptr->st_ino);/*文件i节点号*/
  71.     printf("The file's access mode is:10进制=[%d],8进制=[%o], 16进制=[%X]\n",ptr->st_mode, ptr->st_mode, ptr->st_mode);/*文件的访问模式*/
  72.     printf("The file's hard link number is:%d\n",ptr->st_nlink);/*文件的硬链接数目*/
  73.     printf("The file's user id is:%d\n",ptr->st_uid);/*文件拥有者的ID*/
  74.     printf("The file's group id is:%d\n",ptr->st_gid);/*文件的组ID*/
  75.     printf("The file's size is:%d\n",ptr->st_size);/*文件的大小*/
  76.     printf("The block size is:%d\n",ptr->st_blksize);/*文件占用的块数量*/
  77.     printf("The number of allocated blocks is:%d\n",ptr->st_blocks);/*文件分配块数量*/
  78.     struct tm*accesstime,*lmodifytime,*lchangetime;/*访问时间,修改时间,最后一个改变时间(属性)*/
  79.     accesstime=localtime(&(ptr->st_atime));
  80.     lmodifytime=localtime(&(ptr->st_mtime));
  81.     lchangetime=localtime(&(ptr->st_ctime));
  82.     printf("The last access time is [%02d:%02d:%02d]\n",accesstime->tm_hour,accesstime->tm_min,accesstime->tm_sec);
  83.     printf("The last modify time is [%02d:%02d:%02d]\n",lmodifytime->tm_hour,lmodifytime->tm_min,lmodifytime->tm_sec);
  84.     printf("The last change time is [%02d:%02d:%02d]\n",lchangetime->tm_hour,lchangetime->tm_min,lchangetime->tm_sec);

  85. }

阅读(448) | 评论(0) | 转发(0) |
0

上一篇:日志函数的实现

下一篇:system函数

给主人留下些什么吧!~~