linux中的read函数_linux open函数

linux中的read函数_linux open函数1.首先要打开目录文件DIR*opendir(constchar*name);DIR*fdopendir(intfd);2.读取目录文件信息的函数注意:这是个库函数structdirent*readdir(DIR*dirp);intreaddir_r(DIR*dirp,structdirent*entry,st…

大家好,又见面了,我是你们的朋友全栈君。

1.首先要打开目录文件

DIR *opendir( const char *name);

DIR *fdopendir( int fd);

2.读取目录文件信息的函数    

注意:这是个库函数

struct dirent *readdir( DIR *dirp);

int readdir_r(    DIR *dirp,     struct dirent *entry,    struct dirent **result);

文件目录结构体:

struct dirent {
      ino_t          d_ino;       /* inode number 索引节点号*/
      off_t          d_off;       /* not an offset; see NOTES 在目录文件中的偏移*/
      unsigned short d_reclen;    /* length of this record 文件名长*/
      unsigned char  d_type;   /*type of file; not supported by all  filesystem types 文件类型*/              
                                                                                           
      char           d_name[256]; /* filename 文件名,最长255字符*/
           };

 

d_type的值为:

DT_BLK This is a block device.

DT_CHR This is a character device.

DT_DIR This is a directory.

DT_FIFO This is a named pipe (FIFO).

DT_LNK This is a symbolic link.

DT_REG This is a regular file.

DT_SOCK This is a UNIX domain socket.

DT_UNKNOWN The file type is unknown.

readdir()函数实例:

注意:

每次使用readdir后,readdir会读到下一个文件,readdir是依次读出目录中的所有文件,每次只能读一个

这个特性和readdir_r()一样

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
int main(int argc, char **argv)
 
{
	DIR *pDir = NULL;
	struct dirent * pEnt = NULL;
	unsigned int cnt = 0;	
	if (argc != 2)
	{
		printf("usage: %s dirname\n", argv[0]);
		return -1;
	}
	pDir = opendir(argv[1]);
	if (NULL == pDir)
	{
		perror("opendir");
		return -1;
 
	}	
	while (1)
	{
		pEnt = readdir(pDir);
		if(pEnt != NULL)
		{
			if (pEnt->d_type == DT_REG)
			{
				printf("是普通文件:");
			}
			else
			{
				printf("不是普通文件:");
			}
			printf("name:[%s]	\n", pEnt->d_name);
			cnt++;
		}
		else
		{
			break;
		}
	};
	printf("总文件数为:%d\n", cnt);
	return 0;
}

结果:

$ ./a.out .
是普通文件:name:[a.c]	
不是普通文件:name:[.]	
不是普通文件:name:[..]	
是普通文件:name:[a.out]	
不是普通文件:name:[12_sr]	
不是普通文件:name:[10_sr]	
不是普通文件:name:[17_sr]	
不是普通文件:name:[15_sr]	
不是普通文件:name:[14.sr]	
不是普通文件:name:[18_sr]	
不是普通文件:name:[udp]	
不是普通文件:name:[16_sr]	
不是普通文件:name:[tcp]	
总文件数为:13

readdir_r():

注意:

这三个参数

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
	DIR *pDir = NULL;
	struct dirent * pEnt = NULL;
    	struct dirent *entry = (struct dirent *)malloc(sizeof(struct dirent));
    	struct dirent **result = (struct dirent **)malloc(sizeof(struct dirent));
	unsigned int cnt = 0;
	unsigned int ret = 0;	
	if (argc != 2)
	{
		printf("usage: %s dirname\n", argv[0]);
		return -1;
	}
	pDir = opendir(argv[1]);
	if (NULL == pDir)
	{
		perror("opendir");
		return -1;
	}
	ret = readdir_r(pDir , entry , result);
	printf("return	:%d	\n", ret);
	printf("name	:[%s]	\n", entry->d_name);
	printf("name	:[%s]	\n", result[0]->d_name);
	ret = readdir_r(pDir , entry , result);
	printf("return	:%d	\n", ret);
	printf("name	:[%s]	\n", entry->d_name);
	printf("name	:[%s]	\n", result[0]->d_name);
	return 0;

}

结果:

linux中的read函数_linux open函数

 

 

 

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/125142.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • FPGA中实现对数运算「建议收藏」

    FPGA中实现对数运算「建议收藏」FPGA中实现对数运算主要有三种方法:(1)在外部直接算好对数值,按照数值范围做个表,存在ram里,到时候查表。为了减少表深度,提高资源利用率,可以考虑去掉部分低位数值,损失一定的精度。(2)使用cordic算法求解对数。(3)log10(x)=ln(x)*log10(e),log10(e)是常数可以手动先计算好,用IPCore的话多个乘法器。下面介绍使用IP核fl…

    2025年6月28日
    5
  • phpMyAdmin安装配置教程「建议收藏」

    phpMyAdmin安装配置教程「建议收藏」phpMyAdmin就是一种MySQL的管理工具,安装该工具后,即可以通过Web形式直接管理MySQL数据库,不需要通过执行系统命令来管理,非常适合对数据库操作命令不熟悉的数据库管理者。一、我们从phpMyAdmin官网上下载该软件,然后将该软件压缩放置xampp目录下。​二、复制PHP文件config.sample.inc文件并改名为config.inc,并做如下修改:​注意:因为XAMPP包含了Apache、MySQL、PHP、PERL,它在安装时

    2022年5月31日
    28
  • cygwin的163镜像(转)

    cygwin的163镜像(转)

    2021年9月11日
    111
  • navicat在线生成激活码【最新永久激活】

    (navicat在线生成激活码)最近有小伙伴私信我,问我这边有没有免费的intellijIdea的激活码,然后我将全栈君台教程分享给他了。激活成功之后他一直表示感谢,哈哈~https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~83PV…

    2022年3月27日
    328
  • 怎么新建pytest的ini文件_bikini

    怎么新建pytest的ini文件_bikini前言pytest配置文件可以改变pytest的运行方式,它是一个固定的文件pytest.ini文件,读取配置信息,按指定的方式去运行查看pytest.ini的配置选项pytest-h找到以下

    2022年7月31日
    40
  • mybatis分页查询之sql server–mysql[通俗易懂]

    mybatis分页查询之sql server–mysql[通俗易懂]freemarker.beansKey”location”wasnotfoundoninstanceoforg.springframework.jdbc.UncategorizedSQLException.freemarker.beansKey”location”wasnotfoundoninstanceofcom.microsoft.sqlserver.jdbc.SQLServerException.sqlserver使用mybatis中分页查询时出现故障

    2022年5月12日
    35

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号