使用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;
}