《C 程序设计语言》 第八章 fsize 读取文件夹内容例子的讨论
2016-08-30 23:05
274 查看
阅读“C程序设计语言”第八章有一个例子,在UNIX系统中自己实现文件夹内容的读取等各种操作。
其中有一个函数readdir 中 利用了open 和read函数来读取文件夹,可是我在Centos7 上总是到失败。
查看stackoverflow上有类似的讨论
http://stackoverflow.com/questions/21405048/linux-open-directory-as-a-file
作者做了这个实验,open是可以打开文件夹但是不能调用读取read,总是返回-1
运行结果
最佳解释如下
文件分为 普通文件 和特殊文件。 而Directory可以是普通文件也可以不是。取决于具体的文件系统。 所以才会定义一套通用的接口 opendir readdir 和closedir
而自己去实现readdir的话设计到具体的文件系统。所以就不深入讨论了。
一个UNIX系统下的使用opendir readdir closedir的例子
文件分为 普通文件 和特殊文件。 而Directory可以是普通文件也可以不是。取决于具体的文件系统。 所以才会定义一套通用的接口 opendir readdir 和closedir
而自己去实现readdir的话设计到具体的文件系统。所以就不深入讨论了。
其中有一个函数readdir 中 利用了open 和read函数来读取文件夹,可是我在Centos7 上总是到失败。
查看stackoverflow上有类似的讨论
http://stackoverflow.com/questions/21405048/linux-open-directory-as-a-file
作者做了这个实验,open是可以打开文件夹但是不能调用读取read,总是返回-1
#include <fcntl.h> #include <stdio.h> #include <errno.h> int main(int argc, char* argv[]) { int fd = -1; if (argc!=1) fd=open(argv[1],O_RDONLY,0); else fd=open(".",O_RDONLY,0); if (fd < 0){ perror("file open"); printf("error on open = %d", errno); return -1; } printf("file descriptor is %d\n", fd); char buf[1024]; int n; if ((n=read(fd,buf,1024))>0){ write(1,buf,n); } else { printf("n = %d\n", n); if (n < 0) { printf("read failure %d\n", errno); perror("cannot read"); } } close (fd); return 0; }
运行结果
file descriptor is 3 n = -1 read failure 21 cannot read: Is a directory
最佳解释如下
1down voteaccepted | Files are also called regular filesto distinguish them from special files. Directory or not a regular file. The most common special fileis the directory. The layout of a directory file is defined by the filesystem used. So use opendir to open diretory. |
而自己去实现readdir的话设计到具体的文件系统。所以就不深入讨论了。
1down voteaccepted | Files are also called regular filesto distinguish them from special files. Directory or not a regular file. The most common special fileis the directory. The layout of a directory file is defined by the filesystem used. So use opendir to open diretory. |
#include<stdio.h> #include<dirent.h> int main(int argc,char* agrv[]){ DIR* dp; struct dirent* dirp; if(argc!=2){ printf("usage:lsdirectory_name\n"); //不返回的话,程序会执行出错 return 0; } if((dp=opendir(agrv[1]))==NULL){ printf("cannotopen%s",agrv[1]); //不返回的话,程序会执行出错 return 0; } while((dirp=readdir(dp))!=NULL){ printf("%s\n",dirp->d_name); } closedir(dp); //c语言以非0为真,所以程序执行成功的话返回1,执行失败返回0 return 1; }
而自己去实现readdir的话设计到具体的文件系统。所以就不深入讨论了。
1down voteaccepted | Files are also called regular filesto distinguish them from special files. Directory or not a regular file. The most common special fileis the directory. The layout of a directory file is defined by the filesystem used. So use opendir to open diretory. |
相关文章推荐
- Java 小例子:读取文件内容
- 利用Java的FileReader类读取文件内容的简单例子
- 忙里偷闲写的小例子---读取android根目录下的文件或文件夹
- Android中读取assets文件夹中的子文件夹内容
- c# winform项目treeview控件绑定本地电脑磁盘信息,读取文件夹信息,显示文件夹内文件内容
- JDBC从文件中读取内容写入CLOB字段的例子
- Scanner类的简单用法(这里举个读取控制台输入的内容,然后输出到控制台的简单例子)
- Matlab 读取父文件下子文件夹内容
- java读取文件夹里的所有xml文件内容
- VBA读取可选择文件夹下可选择txt文件内容
- Java读取一个目录(文件夹)下的文件例子
- 读取文件夹或者文件中的内容
- [原创/讨论] Windows 核心编程研究系列之二:读取物理内存地址中的内容
- c++builder 从当前工程文件夹中根据文件名读取内容到Memo中的处理方法
- Java 小例子:按指定的编码读取文本文件内容
- Java 小例子:按指定的编码读取文本文件内容
- Objective-C 读取网页内容的代码例子
- CMD文件中用FOR命令读取文本文件内容到变量,以及和PsExec搭配使用的方法和例子
- 读取同一文件夹下多个txt文件中的特定内容并做统计
- VBA读取文件夹下所有文件夹及文件内容,并以树形结构展示