대학 생활/C
[C] 파일 목록 가져오기(출력) 예제
opid
2014. 10. 15. 17:57
유닉스에서 파일 목록 출력하기
#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <dirent.h> #include <error.h> int main() { DIR *dir; struct dirent *ent; dir = opendir ("./"); if (dir != NULL) { /* print all the files and directories within directory */ while ((ent = readdir (dir)) != NULL) { printf ("%s\n", ent->d_name); } closedir (dir); } else { /* could not open directory */ perror (""); return EXIT_FAILURE; } }
윈도우에서 파일 목록 출력하기
#include <stdio.h> #include <io.h> #include <conio.h> void main() { _finddata_t fd; long handle; int result = 1; handle = _findfirst(".\\*.*", &fd); //현재 폴더 내 모든 파일을 찾는다. if (handle == -1) { printf("There were no files.\n"); return; } while (result != -1) { printf("File: %s\n", fd.name); result = _findnext(handle, &fd); } _findclose(handle); return; }