티스토리 뷰

내가 지정한 디렉터리의 파일을 가져오고 싶을 떄는 io.h에 정의 된 _finddata_t 구조체를 사용한다.


_finddata_t 구조는 다음과 같은 요소가 포함 되어있다. 

 

 unsigned attrib

  파일의 특성


 time_t time_create

  파일 작성 (-1L FAT 파일 시스템에 대한) 시간

 

 time_t time_access

  마지막 파일 액세스 (–1L FAT 파일 시스템에 대 한) 시간


 time_t time_write

  파일에 마지막으로 쓴 시간


 _fsize_t size

  길이 (바이트)


 char name [ _MAX_PATH]

  파일의 이름입니다.



위 파일을 가져올 구조체를 활용해서 다음 아래의 함수를 이용해, 쉽게 파일 목록을 받아올 수 있다.


_findfirst

검색 하고자하는 파일목록의 첫번째 포인트를 찾아 이동한다.

 filespec

  대상 파일 사양

 fileinfo

  파일 정보 버퍼


_findnext

파일목록을 가르키는 포인트 위치를 다음 위치로 이동시킨다.

 handle

  이전 호출에 의해 반환 되는 검색 핸들

 fileinfo

  파일 정보 버퍼


_findclose

응용 프로그램에서 이러한 함수에 의해 사용 되는 리소스를 해제 한다.

 handle

  이전 호출에 의해 반환 되는 검색 핸들


이제 파일목록을 가지고 올 수 있는 함수에 대해 알아 보았다.

c 코드를 작성하여 실행시켜 보자.


#include <stdio.h>
#include <string.h> 
#include <io.h> 
#include <time.h>
#include <stdlib.h>

typedef struct _finddata_t  FILE_SEARCH;

void GetfileList(char* path); 

int main() {

	char path[100] = "C:/opencv";  
	GetfileList(path);

	return 0;
}

void GetfileList(char* path){

	long h_file;
	char search_Path[100];

	FILE_SEARCH file_search;

	sprintf_s(search_Path, "%s/*.*", path); 
	if((h_file = _findfirst(search_Path, &file_search)) == -1L) { 
		printf( "No files in current directory!\n" ); 

	} else {

		do {    
			printf("%s\n", file_search.name);  
			 
		} while (_findnext(h_file, &file_search) == 0);

		_findclose(h_file); 
	}
}  


결과


_finddata_t 를 이용하면 파일 이름 뿐만 아니라 다른 값도 받아올 수 있게된다.

이 코드에 파일의 작성시간까지 출력해보겠다.


시간을 출력하기 위해서는 파일에서 받은 time값을 문자열로 변환하고 현지 표준 시간대 설정을 조정해야한다.

이를 위해서는 ctime_s를 사용해서 쉽게 바꿀 수 있다.


void GetfileList(char* path){

	long h_file;
	char search_Path[100];

	FILE_SEARCH file_search;

	sprintf_s(search_Path, "%s/*.*", path); 
	if((h_file = _findfirst(search_Path, &file_search)) == -1L) { 
		printf( "No files in current directory!\n" ); 

	} else {

		do {   
			char time[30]; 
			ctime_s( time, _countof(time), &file_search.time_write );
			printf( "%-12s %.24s\n", file_search.name, time);
			 
		} while (_findnext(h_file, &file_search) == 0);

		_findclose(h_file); 
	}
}  

결과



'Language > C' 카테고리의 다른 글

[C언어] 문자 관련 함수  (0) 2013.10.09
[C언어] exit()  (0) 2013.10.09
[C언어] memset  (0) 2013.10.09
[C언어] 문자열 함수  (0) 2013.10.05
[C언어] void형 포인터  (0) 2013.10.02
댓글
최근에 올라온 글
최근에 달린 댓글
글 보관함
Total
Today
Yesterday