[已解决][问题]请问linux下用c语言如何输出一个文件的9种属性

软件和网站开发以及相关技术探讨
回复
billycatcat
帖子: 49
注册时间: 2007-10-06 11:02
来自: 广东佛山

[已解决][问题]请问linux下用c语言如何输出一个文件的9种属性

#1

帖子 billycatcat » 2008-03-21 22:06

像这样的一段菜鸟程序
文件名 main.c

代码: 全选



#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>


int main(int argc, char*argv[])
{
    int opt;
    int fildes;
    struct stat file_stat;
    off_t file_size;
    mode_t file_mode;

    if (argc != 3) {    /*参数太少*/
        printf("Too less arguement.\n");
        exit(1);
    }

    if ((fildes = open(argv[2], O_RDONLY)) == -1) {    /*打开文件出错*/
        printf("Open File Error.\n");
        exit(1);
    }

    if ((fstat(fildes, &file_stat)) == -1) {    /*获取文件属性出错*/
        printf("Fstat File Error.\n");
        exit(1);
    }
    
    file_size = file_stat.st_size;
    file_mode = file_stat.st_mode;

    while ((opt = getopt(argc, argv, "nsm")) != -1) {    /*按照第一个参数的匹配进行处理*/
        switch(opt) {
            case 'n': 
                printf("Name:\t%s\n", argv[2]);
                break;
            case 's':
                printf("Size:\t%d\n", file_size);
                break;
            case 'm':
                printf("Mode:\t%d\n", file_mode);
                break;
            case '?':
                printf("Unknow arguement.\n");
                break;
        }
    }
    
    return 0;
}



gcc -o run main.c 没出错

但假如想测试输出文件权限时 run -m file
却只能得到33188 不能得到-rw-r--r--


请问怎样实现这种-rw-r--r--模式的输出
回复