Скачиваний:
38
Добавлен:
10.02.2019
Размер:
1.33 Кб
Скачать
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#define LENGTH 100

FILE* result;
int break_flag;

void print_file(){
	FILE* file = fopen("./result.txt","r");
	if( !file ) return;
	
	char s[100];
	while( fgets(s, 100, file) ){
		printf("%s", s);
	}
	fclose(file);
}

void write_file(char* str){
	if(!result) return;

	strcat(str,"\n");
	fputs( str, result );
}

void list_dir(char* startdir, char sign){
	if(break_flag) return;

	char nextdir[200] = {0};
	strcpy( nextdir, startdir );

	DIR* dir = opendir( startdir );
	if( !dir ) return;

	struct dirent* de = readdir( dir );
	while( de){
		if(strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0){
			int len = strlen( nextdir );
			strcat( nextdir, "/" );
			strcat( nextdir, de->d_name );

			if( sign == de->d_name[0] && strlen(de->d_name) == 5 && strstr(de->d_name,".txt") && de->d_type == DT_REG){
				write_file(nextdir);
				closedir(dir);
				break_flag = 1; 
				return;			
			}
			
			if( de->d_type == DT_DIR ){
				list_dir( nextdir, sign );
				nextdir[len] = '\0';
			}		
		}	
		de = readdir(dir);
	} 
	closedir(dir);
}

int main(){
	char str[LENGTH];
	scanf("%s",str);
	result = fopen("result.txt","w"); 

	int i;
	for(i = 0; i<strlen(str);i++){
		break_flag = 0;
		list_dir("./tmp", str[i]);
	}
	
	fclose(result);
	print_file();	
	return 0;
}
Соседние файлы в папке lab3