IO(使用readdir显示指定目录下的链接文件)

作业:使用readdir来将目录中的链接文件显示出来,并写入到一个文件内部,再将文件中的内容显示到屏幕上
 

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<dirent.h>
#include<unistd.h>
#include<fcntl.h>

int main(int argc, const char *argv[])
{
	DIR *dp;
	struct dirent *dt;
	int filenum=0;
	FILE *fp=fopen(argv[2],"w");
	if(NULL==fp)
	{
		perror("file to open");
		return -1;
	}
	
		dp=opendir(argv[1]);
	if(NULL==dp)
	{
		perror("fail to opendir");
		return -1;
	}
	while(1)
	{
		dt =readdir(dp);
		if(NULL==dt)
		{
			break;
		}
		if(dt->d_type==DT_LNK)
		{
			printf("%s\n",dt->d_name);
			fprintf(fp,"%s\n",dt->d_name);
			filenum++;
		}
	}

	printf("共有链接文件:%d个\n",filenum);

	fclose(fp);
	fp=fopen(argv[2],"r");
	if(NULL==fp)
	{
		perror("file to open");
		return -1;
	}

	char buf[100];
	printf("写入的文件名:\n");
	while(fgets(buf,100,fp)!=NULL)
	{
		fputs(buf,stdout);
	}
	fclose(fp);


	if(closedir(dp)<0)
	{
		perror("fail to closedir");
		return -1;
	}
	


	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值