免费注册 查看新帖 |

Chinaunix

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

[FreeBSD] FreeBSD的stat结构很多字段没有值。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-08-27 11:59 |只看该作者 |倒序浏览
我stat函数取得文件的属性(存放在stat结构体中)。然后在屏幕上打印这些值。发现st_uid、st_atime、st_ctime等字段的值打印不出来。如果我用%d打印,就提示我错误。而换成%l打印没有错误提示,但什么都打印不出来。

另外,我发现有些函数不能调用。比如我用man查到了cftime函数,内容如下:

  1. SYNOPSIS
  2.      #include <time.h>

  3.      int
  4.      cftime(char *s, char *format, const time_t *clock);
复制代码

结果,我在程序中调用时,提示我函数没有声明。不知道是怎么回事。

论坛徽章:
0
2 [报告]
发表于 2007-08-27 14:27 |只看该作者
没有人知道是怎么回事吗?

论坛徽章:
0
3 [报告]
发表于 2007-08-27 14:57 |只看该作者
才三个钟头就受不了啦?

等gvim上来吧。学校的拨号换了锐捷,我现在连不到机器上,没环境试了

论坛徽章:
0
4 [报告]
发表于 2007-08-27 18:02 |只看该作者
嗯,说的没错,要有耐心

论坛徽章:
0
5 [报告]
发表于 2007-08-27 19:06 |只看该作者
#include <stdio.h>
#include <sys/stat.h>

int main(void)
{
  struct stat  str_stat;
  stat("swapfile",&str_stat);
  printf("uid is %d\n", str_stat.st_uid);
  printf("gid is %d\n", str_stat.st_gid);
  printf("access time is %d\n", str_stat.st_atime);
  printf("modify time is %d\n", str_stat.st_mtime);
  printf("change time is %d\n", str_stat.st_ctime);

  return 0;
}

dmz# gcc -o ttt test.c
dmz# ./ttt
uid is 0
gid is 0
access time is 1186733872
modify time is 1186733865
change time is 1186733865

试了一下,都可以啊。LZ哪里不行?把代码贴出来看看?

论坛徽章:
0
6 [报告]
发表于 2007-08-27 19:13 |只看该作者
把时间戳转换成字符串也可以读出来啊:

#include <stdio.h>
#include <sys/stat.h>
#include <time.h>

int main(void)
{
  struct stat  str_stat;
  stat("swapfile",&str_stat);
  printf("uid is %d\n", str_stat.st_uid);
  printf("gid is %d\n", str_stat.st_gid);
  printf("access time is %s\n", asctime(gmtime(&str_stat.st_atime)));
  printf("modify time is %s\n", asctime(gmtime(&str_stat.st_mtime)));
  printf("change time is %s\n", asctime(gmtime(&str_stat.st_ctime)));

  return 0;
}

dmz# gcc -o ttt test.c
dmz# ./ttt
uid is 0
gid is 0
access time is Fri Aug 10 08:17:52 2007

modify time is Fri Aug 10 08:17:45 2007

change time is Fri Aug 10 08:17:45 2007

论坛徽章:
0
7 [报告]
发表于 2007-08-27 20:40 |只看该作者
下面是我的代码,我大概知道是哪步的问题了。应该是调用strftime不正确。
#include <unistd.h>
#include <time.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>


void do_ls(char* pathname)
{
&nbsp;&nbsp;struct stat sbuf;
&nbsp;&nbsp;struct dirent* pdirent;
&nbsp;&nbsp;DIR* pdir;
&nbsp;&nbsp;char ct[30];

&nbsp;&nbsp;if ((pdir = opendir(pathname)) != NULL)
&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;chdir(pathname);
&nbsp;&nbsp;&nbsp;&nbsp;printf("-----%s-----\n", pathname);
&nbsp;&nbsp;&nbsp;&nbsp;while ((pdirent = readdir(pdir)) != NULL)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("%s\t", pdirent->d_name);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (stat(pdirent->d_name, &sbuf) != -1)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strftime(ct, 30, "%Y%m%d%H%M%S", localtime(&sbuf.st_ctime));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("%d\t%s\t%d\n", sbuf.st_size, ct, sbuf.st_uid);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;printf("-------------\n");
&nbsp;&nbsp;&nbsp;&nbsp;printf("\n");
&nbsp;&nbsp;&nbsp;&nbsp;closedir(pdir);
&nbsp;&nbsp;}
&nbsp;&nbsp;else
&nbsp;&nbsp;&nbsp;&nbsp;perror(pathname);
}


[ 本帖最后由 happy1123 于 2007-8-27 21:27 编辑 ]

论坛徽章:
0
8 [报告]
发表于 2007-08-27 21:02 |只看该作者
下面还有:
#include "dols.h"
#include <unistd.h>
#include <time.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>


void do_ls(char* pathname)
{
&nbsp;&nbsp;struct stat sbuf;
&nbsp;&nbsp;struct dirent* pdirent;
&nbsp;&nbsp;DIR* pdir;

&nbsp;&nbsp;if ((pdir = opendir(pathname)) != NULL)
&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;chdir(pathname);
&nbsp;&nbsp;&nbsp;&nbsp;printf("-----%s-----\n", pathname);
&nbsp;&nbsp;&nbsp;&nbsp;while ((pdirent = readdir(pdir)) != NULL)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("%s\t", pdirent->d_name);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (stat(pdirent->d_name, &sbuf) != -1)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("%d\t%s\t%d\n", sbuf.st_size, asctime(gmtime(&sbuf.st_mtime)), sb
uf.st_uid);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;printf("-------------\n");
&nbsp;&nbsp;&nbsp;&nbsp;printf("\n");
&nbsp;&nbsp;&nbsp;&nbsp;closedir(pdir);
&nbsp;&nbsp;}
&nbsp;&nbsp;else
&nbsp;&nbsp;&nbsp;&nbsp;perror(pathname);
}


下面是运行结果:
%./ls1
-----.-----
.       512     (null)  672513120
..      512     (null)  672513120
ls1.c   161     (null)  672513120
ls1     5791    (null)  672513120
dols.h  139     (null)  672513120
dols.c  674     (null)  672513120
makefile        110     (null)  672513120
ls1.o   848     (null)  672513120
dols.o  1356    (null)  672513120
ls1.core        335872  (null)  672513120
-------------

论坛徽章:
0
9 [报告]
发表于 2007-08-27 21:16 |只看该作者
改成下面这样就可以了:
#include <unistd.h>
#include <time.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>


void do_ls(char* pathname)
{
&nbsp;&nbsp;struct stat sbuf;
&nbsp;&nbsp;struct dirent* pdirent;
&nbsp;&nbsp;DIR* pdir;
&nbsp;&nbsp;char ct[30];

&nbsp;&nbsp;if ((pdir = opendir(pathname)) != NULL)
&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;chdir(pathname);
&nbsp;&nbsp;&nbsp;&nbsp;printf("-----%s-----\n", pathname);
&nbsp;&nbsp;&nbsp;&nbsp;while ((pdirent = readdir(pdir)) != NULL)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("%s\t", pdirent->d_name);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (stat(pdirent->d_name, &sbuf) != -1)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strftime(ct, 30, "%Y%m%d%H%M%S", localtime(&sbuf.st_ctime));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("%s\t%d\t%d\n", ct, sbuf.st_size,  sbuf.st_uid);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;printf("-------------\n");
&nbsp;&nbsp;&nbsp;&nbsp;printf("\n");
&nbsp;&nbsp;&nbsp;&nbsp;closedir(pdir);
&nbsp;&nbsp;}
&nbsp;&nbsp;else
&nbsp;&nbsp;&nbsp;&nbsp;perror(pathname);
}

为什么得到的日期字符串必须放在前面才能打印呢?
比较一下:
     strftime(ct, 30, "%Y%m%d%H%M%S", localtime(&sbuf.st_ctime));
        printf("%d\t%s\t%d\n", sbuf.st_size, ct, sbuf.st_uid);

        strftime(ct, 30, "%Y%m%d%H%M%S", localtime(&sbuf.st_ctime));
        printf("%s\t%d\t%d\n", ct, sbuf.st_size,  sbuf.st_uid);

[ 本帖最后由 happy1123 于 2007-8-27 21:28 编辑 ]

论坛徽章:
0
10 [报告]
发表于 2007-08-28 11:32 |只看该作者
st_size是长整型吧,应该用%ld
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP