C 언어를 이용해서 특정 디렉토리에 존재하는 파일의 총 갯수를 알아내고 싶습니다. 어떻게 하면 될까요?
그냥 함수 몇개만 쓰시면 쉽게 찾을 수 있을꺼 같은데요.
#include <stdio.h> #include <dirent.h> #include <sys/types.h> int main(void) { int file_count = 0; char *dir_path = "/home/test/test_dir/"; struct dirent *dir_ent; DIR *dp; if ((dp = opendir(dir_path)) == NULL) { fprintf(stderr, "opendir() error!\n"); exit(EXIT_FAILURE); } while ((dir_ent = readdir(dp)) != NULL) { if (strcmp(dir_ent->d_name, ".") == 0 || strcmp(dir_ent->d_name, "..") == 0) continue; ++file_count; } printf("File Count = %d\n", file_count); closedir(dp); exit(EXIT_SUCCESS); }
간단하게 이정도면 될꺼 같네요.
H/W가 컴퓨터의 심장이라면 S/W는 컴퓨터의 영혼이다!
seekdir/telldir 함쑤 쌍을 쓸 수도 있을 듯 싶네요.
텍스트 포맷에 대한 자세한 정보
<code>
<blockcode>
<apache>
<applescript>
<autoconf>
<awk>
<bash>
<c>
<cpp>
<css>
<diff>
<drupal5>
<drupal6>
<gdb>
<html>
<html5>
<java>
<javascript>
<ldif>
<lua>
<make>
<mysql>
<perl>
<perl6>
<php>
<pgsql>
<proftpd>
<python>
<reg>
<spec>
<ruby>
<foo>
[foo]
음...
그냥 함수 몇개만 쓰시면 쉽게 찾을 수 있을꺼 같은데요.
간단하게 이정도면 될꺼 같네요.
H/W가 컴퓨터의 심장이라면 S/W는 컴퓨터의 영혼이다!
seekdir/telldir 함쑤 쌍을 쓸 수도 있을 듯 싶네요.
seekdir/telldir 함쑤 쌍을 쓸 수도 있을 듯 싶네요.
댓글 달기