注册 登录
编程论坛 Android开发

android c stat()函数

ujsmk 发布于 2013-11-05 15:38, 3831 次点击
各位大虾,
     目前用C写了一个关于stat()的程序,通过NDK编译后放入android模拟器中运行,发现所获得st_size大小不正确,同样的代码在linux下运行正确,不知如需要使用stat需要注意什么。源码如下:
    #include   <stdio.h>
#include   <unistd.h>
#include   <time.h>
#include   <sys/types.h>
#include   <sys/stat.h>
#include   <grp.h>
#include   <pwd.h>
int main(int argc,char *argv[])
{
        struct stat buf;
        if(argc!=2)
        {
        
            printf("input the parameter error!\n");
            exit(0);
      }
 
        if(stat(argv[1],&buf)==-1)
        {
            printf("get the stat is faild!\n");
            exit(0);
      }
        
        printf("st_dev:%d\n",buf.st_dev);
        printf("st_ino:%d\n",buf.st_ino);
        printf("st_mode:%d\n",buf.st_mode);
        printf("st_nlink:%d\n",buf.st_nlink);
        printf("st_uid:%d\n",buf.st_uid);
        printf("st_gid:%d\n",buf.st_gid);
        printf("st_rdev:%d\n",buf.st_rdev);
        printf("st_size:%ld\n",buf.st_size);
        printf("st_blksize:%d\n",buf.st_blksize);
        printf("st_atime:%s\n",ctime(&buf.st_atime));
        printf("st_mtime:%s\n",ctime(&buf.st_mtime));
        printf("st_ctime:%s\n",ctime(&buf.st_ctime));
        
        struct passwd *psd; //从该结构体中获取文件所有者的用户名
        struct group  *grp; //从该结构体中获取文件所有者的组名
        psd=getpwuid(buf.st_uid);
        grp=getgrgid(buf.st_gid);
        printf("user name:%-9s\n",psd->pw_name); // 打印文件拥有者
        printf("group name:%-8s\n",grp->gr_name); //打印文件所属用户组
        return 0;
}
    运行结果:
root@android:/data/g132/test # ./fileattributes tttt.txt
./fileattributes tttt.txt
st_dev:-1092224304
st_ino:40180
st_mode:33206
st_nlink:1
st_uid:0
st_gid:0
st_rdev:40244
st_size:40256
st_blksize:4096
st_atime:Tue Nov  5 02:32:53 2013

st_mtime:Tue Nov  5 03:37:39 2013

st_ctime:Tue Nov  5 03:37:39 2013

user name:root
group name:root
root@android:/data/g132/test #
0 回复
1