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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 免费好用的图片压缩网站,有这五个就够了(收藏备用)

    免费好用的图片压缩网站,有这五个就够了(收藏备用)前言当我们想要线上报名提交照片时,大小却超过范围,这时就需要压缩软件的帮助。但大多主流的软件可以压缩但想要下载就需要收费了,下面是五个国内外免费的压缩平台。一、OnlineImage优点:图片可以压缩90%以上,可以批量压缩,速度只需要几秒钟完全免费,无数量限制,支持绝大多数图片压缩格式缺点:不支持自定义图片大小压缩,广告比较多网站链接为:https://www.onlineimagetool.com/zh/二、Docsmall……

    2022年6月18日
  • 三极管是如何导通的?「建议收藏」

    三极管是如何导通的?「建议收藏」作者:被吊打的学渣链接:https://www.zhihu.com/question/19998995/answer/311658942来源:知乎著作权归作者所有。商业转载请联系作者获得授权,非商业转

    2022年8月3日
  • EPPlus使用

    EPPlus使用看看我公式计算excel中离不开各种各样的公式计算,在Epplus中运用公式有两种方式,你都可以尝试一下:worksheet.Cells[“D2:D5″].Formula=”B2*C2”;//这是乘法的公式,意思是第二列乘以第三列的值赋值给第四列,这种方法比较简单明了worksheet.Cells[6,2,6,4].Formula=string.Format(“SUBT…

    2022年6月16日
  • fastjson object转jsonobject_jsonobject取值

    fastjson object转jsonobject_jsonobject取值众所周知,kafka中存储的数据是经过BASE64加密后的jsonObject,因此从kafka中读取的数据经过base64解码,得到的是json串,利用JSONObect的方法可以对json串进行解析,拿到对应的数据。那么要如何将scala对象或者java对象转换为JsonObject对象或JSONObject对象呢?(注意:JsonObject对象和JSONObject对象不同,调用的API也…

    2022年10月5日
  • Vlc for Android 全面阐述

    Vlc for Android 全面阐述

    2022年1月30日
  • CPLD与FPGA的区别

    CPLD与FPGA的区别FPGA和CPLD是两种著名的数字逻辑芯片。当涉及到内部架构时,这两种芯片显然是不同的。FPGA:现场可编程门阵列,是一种可编程逻辑芯片。它是一个伟大的芯片,因为它可以被编程去做几乎任何一种数字功能。FPGA的架构允许芯片具有很高的逻辑容量。它被用于设计要求很高的门数和它们的延迟是相当不可预测的,因为它的结构。FPGA被认为是“细粒”,因为它包含了很多可以达到10万的微小逻辑块。这是人组合逻辑和记…

    2022年5月4日

发表回复

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

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