Reference:
stat(2) - 파일의 상태 및 정보를 얻는 함수
stat(2) #include #include #include int stat(const char *path, struct stat *buf); 파일의 크기, 파일의 권한, 파일의 생성일시, 파일의 최종 변경일 등, 파일의 상태나 파일의 정보를 얻는 함수입니다. stat(2) 함수는 s
www.it-note.kr
#include <errno.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <stdio.h> #include <time.h> #if 0 struct stat { __dev_t st_dev; /* Device. */ __ino_t st_ino; /* File serial number */ __nlink_t st_nlink; /* Link count. */ __mode_t st_mode; /* File mode. */ __uid_t st_uid; /* User ID of the file's owner. */ __gid_t st_gid; /* Group ID of the file's group. */ __dev_t st_rdev; /* Device number, if device. */ __off_t st_size; /* Size of file, in bytes. */ __blksize_t st_blksize; /* Optimal block size for I/O */ __blkcnt_t st_blocks; /* Number 512-byte blocks allocated. */ /** * Nanosecond resolution timestamps are stored in a format * equivalent to 'struct timespec'. * This is the type used whenever possible * but the Unix namespace rules do not allow the identifier 'timespec' * to appear in the <sys/stat.h> header. * Therefore we have to handle the use of this header * in strictly standard-compliant sources sepcial. */ time_t st_atime; /* time of last access */ time_t st_mtile; /* time of last modification */ time_t st_ctime; /* time of last status change */ }; int stat(const char *path, struct stat *buf); #endif inline bool fs_file_exists(const char *path) { struct stat buffer; return stat(path, &buffer) == 0; } int example(const char *path) { struct stat sb; if (stat(path, &sb) == -1) { // 오류, 상세한 오류 내용은 errno 전역변수에 설정 perror("stat"); return errno; } switch (sb.st_mode & S_IFMT) { case S_IFBLK: printf("block device\n"); break; case S_IFCHR: printf("character device\n"); break; case S_IFDIR: printf("directory\n"); break; case S_IFIFO: printf("FIFO/pipe\n"); break; case S_IFLNK: printf("symlink\n"); break; case S_IFREG: printf("regular file\n"); break; case S_IFSOCK: printf("socket\n"); break; default: printf("unknown?\n"); break; } printf("I-node number: %ld\n", (long) sb.st_ino); printf("Mode: %lo (octal)\n", (unsigned long) sb.st_mode); printf("Link count: %ld\n", (long) sb.st_nlink); printf("Ownership: UID=%ld GID=%ld\n", (long) sb.st_uid, (long) sb.st_gid); printf("Preferred I/O block size: %ld bytes\n", (long) sb.st_blksize); printf("File size: %lld bytes\n", (long long) sb.st_size); printf("Blocks allocated: %lld\n", (long long) sb.st_blocks); printf("Last status change: %s", ctime(&sb.st_ctime)); printf("Last file access: %s", ctime(&sb.st_atime)); printf("Last file modification: %s", ctime(&sb.st_mtime)); return 0; }
#include <iostream> #include <string> #include <sys/stat.h> // stat #include <errno.h> // errno, ENOENT, EEXIST #if defined(_WIN32) # include <direct.h> // _mkdir #endif bool fs_file_exists(const std::string& path) { #if defined(_WIN32) struct _stat info; return !_stat(path.c_str(), &info); #else struct stat info; return !stat(path.c_str(), &info); #endif } bool fs_dir_exists(const std::string& path) { #if defined(_WIN32) struct _stat info; return _stat(path.c_str(), &info) ? false : (info.st_mode & _S_IFDIR) != 0; #else struct stat info; return stat(path.c_str(), &info) ? false : (info.st_mode & S_IFDIR) != 0; #endif } bool fs_mkdir(const std::string& path) { #if defined(_WIN32) int rv = _mkdir(path.c_str()); #else mode_t mode = 0755; int rv = mkdir(path.c_str(), mode); #endif if (rv) { switch (errno) { case ENOENT: // parent didn't exist, try to create it { int pos = path.find_last_of('/'); if (pos == std::string::npos) #if defined(_WIN32) pos = path.find_last_of('\\'); if (pos == std::string::npos) #endif return false; if (!fs_mkdir(path.substr(0, pos))) return false; } // now, try to create again #if defined(_WIN32) return !_mkdir(path.c_str()); #else return !mkdir(path.c_str(), mode); #endif case EEXIST: // done! return fs_dir_exists(path); default: return false; } } return true; }
'OS > Linux' 카테고리의 다른 글
LD_LIBRARY_PATH (0) | 2022.12.21 |
---|---|
CUDA 11.7.1 on WSL2 (0) | 2022.11.13 |
Install libjpeg-turbo (0) | 2022.11.06 |
CUDA-11.4 on WSL2 (0) | 2022.10.12 |
Ubuntu에서 GPG ERROR NO_PUBKEY 해결방법 (0) | 2022.10.11 |