20230411华清远见+IO

4 篇文章 0 订阅

使用readdir来将目录中的链接文件显示出来,并写入到一个文件内部,再将文件中的内容显示到屏幕上
在这里插入图片描述


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<errno.h>
#include <sys/types.h>
#include <dirent.h>


int main(int argc, const char *argv[])
{
	FILE *dest_file;//目的文件
	DIR *dp;
	struct dirent *dt;
	int count = 0;
	char ch;

//打开目录,写入链接目录名到新的文件
	dp = opendir(argv[1]);
	if(NULL == dp){
		perror("Fail to open\n");
		return -1;
	}
	dest_file = fopen(argv[2],"w+");
	if(dest_file == NULL){
		printf("写入文件失败\n");
		return -1;
	}

	//输出目录中的链接的文件
	while(1){
		dt = readdir(dp);
		if(NULL == dt){
			break;
		}
		if(dt->d_type == 10){   // 找到链接文件
			fprintf(dest_file,"链接文件名:%s,链接文件的类型是:%d\n",dt->d_name,dt->d_type);
			count++;
		}
	}
//将显示出来的链接文件名写入别的文件内
	fseek(dest_file,0,SEEK_SET);
	while(EOF != fscanf(dest_file,"%c",&ch)){
		printf("%c",ch);
		ch = 0;
	}
	printf("\n");
	printf("%d\n",count);//
//输出该文件内容


//关闭目录
	if(closedir(dp)<0){
		perror("Fail to closedir\n");
		return -1;
	}

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值