컴퓨터 엑셀 워드 포토샵 구글어스 WINDOWS JAVASCRIPT JAVA C++

 
Tuesday, April 03, 2007

C언어] 파일 존재 여부 판단, 디렉토리 있는지? 함수; Is File Directory Exists


C에서, 파일이나 디렉토리(폴더)가 하드에 실제로 존재하는지 아닌지 체크하는 방법입니다.

isFileExists() 함수로, 파일이 존재하는지 아닌지 알아내고
isDirExists() 함수로는, 디렉토리가 실재하는지 판단할 수 있습니다.

그런데 이런 작업은 컴파일러 종류마다 다릅니다. 다음은 비주얼C에서 사용할 수 있는 방법입니다.

VC: 파일/폴더 존재 여부 알아내기 예제 소스


소스 파일명: example.cpp
#include <stdio.h>
#include <io.h>

// 파일이 존재하는지 판단 함수
int isFileExists(char* s);

// 디렉토리 존재 여부 판단 함수
int isDirExists(char* s);



int main(void) {
  char* filename = "test.txt";
  char* dirname = "000";


  if (isFileExists(filename) == 1)
    puts("파일 있습니다.");
  else
    puts("그런 파일은 없습니다.");


  if (isDirExists(dirname) == 1)
    puts("디렉토리 있습니다.");
  else
    puts("그런 디렉토리는 없습니다.");

  return 0;
}




int isFileExists(char* s) {
  _finddatai64_t c_file;
  intptr_t hFile;
  int result = 0;


  if ( (hFile = _findfirsti64(s, &c_file)) == -1L )
    result = 0; // 파일 없으면 거짓 반환
  else
    if (c_file.attrib & _A_SUBDIR) result = 0; // 있어도 디렉토리면 거짓
  else
    result = 1; // 그밖의 경우는 "존재하는 파일"이기에 참


  _findclose(hFile);

  return result;
}



int isDirExists(char* s) {
  _finddatai64_t c_file;
  intptr_t hFile;
  int result = 0;

  hFile = _findfirsti64(s, &c_file);
  if (c_file.attrib & _A_SUBDIR ) result = 1;
  _findclose(hFile);

  return result;
}




▶▶ C언어] 파일인지 디렉토리인지 여부 판단 함수; is File or Dir




tag: cpp
C언어 | C/C++ (Visual C++)

0 Comments:

Post a Comment

<< Home RSS 2.0 feed

구글 Google 에서 제공하는 무료 블로그 서비스인 블로거 Blogger 의 인터넷 주소는 www.blogger.com 입니다. Blogger 에 블로그를 만들면, blogspot.com 이라는 주소에 블로그가 생성됩니다.
블로그를 직접 방문하지 않고도 최신 게시물을 구독하려면 RSS 2.0 feed 주소를 리더기에 등록하시면 됩니다.
Previous Posts
Monthly Archives
Top