기계는 거짓말하지 않는다

C 파일 존재 여부, File Exist Check 본문

C

C 파일 존재 여부, File Exist Check

KillinTime 2022. 5. 13. 17:38

C언어 파일 존재여부 확인

Linux

#include <unistd.h>
 
if( access( f_path, F_OK ) != -1 ) {
    // 파일 존재
} else {
    // 파일 없음
}

stat 구조체 이용

#include <sys/stat.h>

bool exist_file (char *file_name) {
  struct stat   buffer;   
  return (stat (file_name, &buffer) == 0);
}

FILE 구조체 이용

#include <stdio.h>

FILE *file;

if (file = fopen("temp.txt", "r")) 
{
    fclose(file);
    printf("파일 존재\n");
}
else
{
    printf("파일 없음\n");
}
Comments