Linux编程中目录内容的读取(实现ls功能)
来源:互联网 发布:网游之演技一流张知 编辑:程序博客网 时间:2024/06/18 04:06
dirent.h是POSIX.1标准定义的unix类目录操作的头文件,包含了许多UNIX系统服务的函数原型。
头文件的位置在/usr/include/dirent.h,里面有很多函数定义和宏,可以提供给我们非常丰富的目录处理方法。我们在编程中一般只需要简单的使用下面几个函数。
/* This is the data type of directory stream objects. The actual structure is opaque to users. */ typedef struct __dirstream DIR; /* Open a directory stream on NAME. Return a DIR stream on the directory, or NULL if it could not be opened. This function is a possible cancellation point and therefore not marked with __THROW. */ extern DIR *opendir (__const char *__name) __nonnull ((1));/* Close the directory stream DIRP. Return 0 if successful, -1 if not. This function is a possible cancellation point and therefore not marked with __THROW. */extern int closedir (DIR *__dirp) __nonnull ((1));/* Read a directory entry from DIRP. Return a pointer to a `struct dirent' describing the entry, or NULL for EOF or error. The storage returned may be overwritten by a later readdir call on the same DIR stream.If the Large File Support API is selected we have to use the appropriate interface. This function is a possible cancellation point and therefore not marked with __THROW. */#ifndef __USE_FILE_OFFSET64extern struct dirent *readdir (DIR *__dirp) __nonnull ((1));#else
示例代码,实现ls功能(简单实现了-a参数):
#include <stdio.h>#include <dirent.h>#include <stdlib.h>#define FLAG_NOHIDE 0x01int main(int argc, char *argv[]){ DIR *dp; struct dirent *dirp; int opt; unsigned char flag=0x00; if (argc < 2) { printf("usage: ls directory_name\n"); exit(0); } while ((opt = getopt(argc, argv, "a")) != -1) { switch (opt) { case 'a': flag |= FLAG_NOHIDE; break; } } // open the dir(the 2ed argv index is always argc-1 I guess) if ((dp = opendir(argv[argc-1])) == NULL) { printf("error: can't open %s\n", argv[argc-1]); exit(0); } // use readdir to get all the file struct while ((dirp = readdir(dp)) != NULL) { if (!(flag & FLAG_NOHIDE)) { if (dirp->d_name[0] == '.') { continue; } } printf("%s\n", dirp->d_name); } closedir(dp); return 0;}
附上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) 文件名,最长256字符 */}
1 0
- Linux编程中目录内容的读取(实现ls功能)
- (八)linux下编程实现ls功能的打开、访问 、关闭目录以及提取属性。
- Linux系统编程_1_目录读取(实现简单ls命令)
- linux中读取目录的内容
- 文件目录操作编程 实现“ls -l“功能
- "ls"功能的实现
- Linux C编程学习笔记(5):文件属性操作和目录操作,ls的实现
- linux编程:ls命令的简单实现
- 实现ls -l功能 和目录实现
- 编程实现 ls -l 命令所实现的功能
- 编程实现UNIX下ls-a的功能
- linux中目录信息查询ls命令的简单使用方法
- Linux下用C语言实现ls -l的功能
- linux文件和目录操作(附ls命令实现)
- ls -l功能的实现
- 【ls】查看文件夹中内容,查看当前目录内容
- 用C实现linux下的ls功能(针对单一文件)
- linux中目录处理命令 ls
- centos7下安装fastdfs
- 转场动画 学习
- 我的CentOS环境部署过程:从vmware安装到环境搭建
- qt 使用xlslib操作execl
- HTTP的GET请求中文乱码
- Linux编程中目录内容的读取(实现ls功能)
- iOS 真机 Could not launch "***"
- Spring的AOP支持 使用Annotation方式
- 正则表达式
- Windows编程C++之句柄
- spark的task调度器(FIFO先进先出调度算法)
- bugfree配置和使用
- Installshield 打包安装包心得
- JetBrains宣布Project Rider:一款C#跨平台IDE