免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 7708 | 回复: 8
打印 上一主题 下一主题

linux 下获得文件 生成时间 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-04-20 09:20 |只看该作者 |倒序浏览
[root@localhost .vnc]# ls -l
总用量 48
-rw-r--r--    1 root     root         1281  4月 20 09:12 localhost.localdomain:1.log
-rw-r--r--    1 root     root         1359  4月 20 09:09 localhost.localdomain:2.log


[root@localhost .vnc]#

我想获取 /root/.vnc下 最晚生成的后缀为.log的文件的名字 这个例子就是 1.log   用c实现 该怎么搞啊
请大家帮忙!!!

论坛徽章:
0
2 [报告]
发表于 2007-04-20 09:22 |只看该作者
楼主一定要用c搞么

论坛徽章:
0
3 [报告]
发表于 2007-04-20 09:26 |只看该作者
是的,本来想自动获取 vnc 的端口号的 通过 日志文件的文件名获得 所以必须得到最晚生成的日志的文件名

论坛徽章:
0
4 [报告]
发表于 2007-04-20 10:02 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
5 [报告]
发表于 2007-04-20 10:06 |只看该作者
论坛里面搜索,讨论过 N 次了。

论坛徽章:
0
6 [报告]
发表于 2007-04-20 10:07 |只看该作者
谢谢各位

论坛徽章:
0
7 [报告]
发表于 2007-04-20 10:21 |只看该作者
首先,用opendir函数打开/root/.vnc目录,然后用readdir函数来获得这个目录下的各个文件,
readdir获得的结构是:
struct dirent {
              ino_t          d_ino;       /* inode number */
              off_t          d_off;       /* offset to the next dirent */
              unsigned short d_reclen;    /* length of this record */
              unsigned char  d_type;      /* type of file */
              char           d_name[256]; /* filename */
};
这个里面,d_name就是文件名。通过文件名来判断一个文件是不是.log文件,
然后使用stat函数来获得这个文件的属性,获得的属性为:
          struct stat {
              dev_t     st_dev;     /* ID of device containing file */
              ino_t     st_ino;     /* inode number */
              mode_t    st_mode;    /* protection */
              nlink_t   st_nlink;   /* number of hard links */
              uid_t     st_uid;     /* user ID of owner */
              gid_t     st_gid;     /* group ID of owner */
              dev_t     st_rdev;    /* device ID (if special file) */
              off_t     st_size;    /* total size, in bytes */
              blksize_t st_blksize; /* blocksize for filesystem I/O */
              blkcnt_t  st_blocks;  /* number of blocks allocated */
              time_t    st_atime;   /* time of last access */
              time_t    st_mtime;   /* time of last modification */
              time_t    st_ctime;   /* time of last status change */
          };
由于Linux下面不存储文件建立的时间,所以只能用st_atime, st_mtime,st_ctime来
比较,你可以找到时间最大的那个,可以当作最后生成的文件。
time_t可以直接当作整形来比较大小。

我考虑到的基本上就是这个步骤了。

论坛徽章:
0
8 [报告]
发表于 2007-04-20 11:00 |只看该作者
int getFile(char fileName[][64])
{
        struct ffblk ff;
        int done;
        printf("Directory listing of /root/.vnc/*.log\n");
        done = findfirst("/root/.vnc/*.log", ff, 0);
        int i = 0;
        while (!done)
        {
                strcpy(fileName[i], ff.ff_name);
                printf(" %s\n", ff.ff_name);
                done = findnext(ff);
                i ++;
        }
        return i;
}

参考了别的文章 写了这个 但是 cc 报错 storage size of ff isn't known
不知道为什么

论坛徽章:
0
9 [报告]
发表于 2007-04-20 11:30 |只看该作者
stat    fstat     lstat  
struct   stat{...}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP