Programming/C/C++
C/Linux 파일 정보 검색
현벨
2017. 11. 2. 15:22
반응형
파일 정보 검색 - 파일명으로 파일 정보 검색 stat(1)
- inode에 저장된 파일 정보 검색
- path에 검색할 파일의 경로를 지정하고, 검색한 정보를 buf에 저장
- stat 구조체
inode.c - stat함수 사용
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include <sys/types.h> #include <sys/stat.h> #include <stdio.h> int main(void) { struct stat buf; stat("unix.txt", &buf); printf("Inode = %d\n", (int)buf.st_ino); printf("Mode = %o\n", (unsigned int)buf.st_mode); printf("Nlink = %o\n", (unsigned int) buf.st_nlink); printf("UID = %d\n", (int)buf.st_uid); printf("GID = %d\n", (int)buf.st_gid); printf("SIZE = %d\n", (int)buf.st_size); printf("Atime = %d\n", (int)buf.st_atime); printf("Mtime = %d\n", (int)buf.st_mtime); printf("Ctime = %d\n", (int)buf.st_ctime); printf("Blksize = %d\n", (int)buf.st_blksize); printf("Blocks = %d\n", (int)buf.st_blocks); return 0; } |
실행 결과
파일 정보 검색 - 파일 기술자로 파일 정보 검색 fstat(2)
- 저수준 입출력방식
- fd로 지정한 파일의 정보를 검색하여 buf에 저장
fstat.c - 파일 기술자로 정보 검색
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | #include <sys/stat.h> #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <unistd.h> int main(){ int fd; struct stat buf; fd = open("unix.txt", O_RDONLY); if (fd == -1){ perror("open error : unix.txt"); exit(1); } fstat(fd, &buf); printf("Inode = %d\n", (int)buf.st_ino); printf("Mode = %o\n", (unsigned int)buf.st_mode); printf("Nlink = %o\n", (unsigned int) buf.st_nlink); printf("UID = %d\n", (int)buf.st_uid); printf("GID = %d\n", (int)buf.st_gid); printf("SIZE = %d\n", (int)buf.st_size); printf("Atime = %d\n", (int)buf.st_atime); printf("Mtime = %d\n", (int)buf.st_mtime); printf("Ctime = %d\n", (int)buf.st_ctime); printf("Blksize = %d\n", (int)buf.st_blksize); printf("Blocks = %d\n", (int)buf.st_blocks); close(fd); return 0; } |
실행 결과
반응형