• linux c编程:文件夹操作


    创建目录:

    mkdir函数创建目录:

    mkdir(const char *pathname, mode_t mode)

    参数mode有下列的组合:

    S_ISUID 04000 文件的执行时设置用户IDset user-id on execution)位  

    S_ISGID 02000 文件的执行时设置组IDset group-id on execution)位  

    S_ISVTX 01000 文件的保存正文(粘着位sticky)  

    S_IRWXU            00700 文件所有者具有读、写、执行权限  

    S_IRUSRS_IREAD 00400 文件所有者具可读取权限  

    S_IWUSRS_IWRITE00200 文件所有者具可写入权限   

    S_IXUSRS_IEXEC 00100 文件所有者具可执行权限  

    S_IRWXG 00070 用户组具有读、写、执行权限  

    S_IRGRP 00040 用户组具可读取权限  

    S_IWGRP 00020 用户组具可写入权限  

    S_IXGRP 00010 用户组具可执行权限  

    S_IRWXO 00007 其他用户具有读、写、执行权限  

    S_IROTH 00004 其他用户具可读取权限  

    S_IWOTH 00002 其他用户具可写入权限  

    S_IXOTH 00001 其他用户具可执行权限

    代码如下

    #include <unistd.h>

    #include <sys/stat.h>

    void make_dir_function(){

    const char *pathname="/home/zhf/c_test";

    mkdir(pathname,0744);

    }

    读目录:

    DIR *opendir(const char *pathname); 获取path子目录下的所由文件和目录的列表,如果path是个文件则返回值为NULL

    DIR 结构体的原型为:struct_dirstreamlinux系统中:typedef struct __dirstream DIR;

           struct __dirstream
           {
             void *__fd; /* `struct hurd_fd' pointer for descriptor.   */
             char *__data; /* Directory block.   */
             int __entry_data; /* Entry number `__data' corresponds to.   */
             char *__ptr; /* Current pointer into the block.   */
             int __entry_ptr; /* Entry number `__ptr' corresponds to.   */
             size_t __allocation; /* Space allocated for the block.   */
             size_t __size; /* Total valid data in the block.   */
             __libc_lock_define (, __lock) /* Mutex lock for this structure.   */
            };

    struct dirent *readdir(DIR *dp); 读取opendir 返回值的那个列表. 返回dirent结构提指针,dirent结构体成员如下:

    struct dirent 

      

      long d_ino; /* inode number 索引节点号 */ 

      off_t d_off; /* offset to this dirent 在目录文件中的偏移 */ 

      unsigned short d_reclen; /* length of this d_name 文件名长 */ 

      unsigned char d_type; /* the type of d_name 文件类型 */ 

      char d_name [NAME_MAX+1]; /* file name (null-terminated) 文件名,最长255字符 */ 

    代码如下:

    void read_files_in_dir(){

    DIR *dirptr=NULL;

    struct dirent *entry;

    dirptr=opendir("/home/zhf/c_prj");

    while(entry=readdir(dirptr)){

    printf("The file name is %s ",entry->d_name);

    }

    closedir(dirptr);

    }

    运行结果:

    The file name is function.o

    The file name is chapter3.c

    The file name is main.c

    The file name is file.hole

    The file name is chapter3

    The file name is file_function

    The file name is file_function.c

    The file name is main.o

    The file name is offer_test.c

    The file name is .

    The file name is function.c

    The file name is ..

    The file name is main

    The file name is offer_test

     

    目录修改:

    每个进程都有一个当前工作目录,进程调用chdirfchdir函数可以更改当前工作目录

    下面这段代码修改工作目录为/home/zhf/c_prj

    void change_dir_function(){

    char *ptr;

    size_t size;

    chdir("/home/zhf/c_prj");

    }

    但是当我们在shell中用pwd去查询的时候发现工作目录还是/home/zhf,修改工作目录并没有生效

    root@zhf-maple:/home/zhf# pwd

    /home/zhf

    这是因为每个程序运行在独立的进程中,shell的当前工作目录并不会随着程序调用chdir而改变。所以为了改变shell进程自己的工作目录,shell应当直接调用chdir函数。那么在代码中修改了工作目录如何查看呢? 这就需要用到getcwd函数

    char *getcwd(char *buf,size_t size)  若成功,返回buf, 若出错,返回Null

    代码如下

    void change_dir_function(){

    char *ptr;

    size_t size;

    chdir("/home/zhf/c_prj");

    ptr=(char *)malloc(sizeof(size));

    getcwd(ptr,size);

    printf("the work directory is %s ", ptr);

    }

    the work directory is /home/zhf/c_pr

  • 相关阅读:
    你能用多长时间停车?
    中国威胁论好像还挺严重的
    热爱生命
    lunix下shell脚本批量获取文件,批量修改文件内容
    sql数据操作的若干心得(二)向表中自动插入自增的ID
    Asp.net开发之旅动态产生控件
    Asp.net开发之旅GridView中嵌入DropDownList的一点心得
    Asp.net开发之旅开发环境
    Asp.net开发之旅简单的引用母版页
    Sql数据操作的若干心得
  • 原文地址:https://www.cnblogs.com/zhanghongfeng/p/8902234.html
Copyright © 2020-2023  润新知