Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 균형잡힌 세상
- 브런치
- Process Communication
- 1874
- 4949
- For Beginners
- 입력 버퍼
- QA
- The Balance of the World
- Zero That Out
- IT
- 10773
- c++
- 백준
- 바샤
- file IO
- 해바
- system programming
- 2941
- 전자책
- BAKA
- c
- 시프
- Parenthesis
- LJESNJAK
- 시스템 프로그래밍
- 5622
- Baekjoon
- process control
- File 조작
Archives
- Today
- Total
해바
Lab #6 본문
다음과 같은 프로그램을 작성하라 :
- 인자로서 디렉토리 이름과 파일의 접미사(suffix)를 읽어 들인다.
- 주어진 디렉토리를 탐색하여 이름에 인자로 주어진 접미사가 포함된 첫번째 파일을 찾아 그 파일의 i-node 값과 파일 이름을 출력한다.
- 주어진 파일이름(*s1)이 접미사 (*s2)를 포함하는지 check 해 주는 함수 int match(char* s1, char* s2)를 활용할 것
int match(char* s1, char* s2) {
int diff = strlen(s1) - strlen(s2);
if(strlen(s1) > strlen(s2)) return (strcmp(&s1[diff], s2) == 0);
else return 0;
}
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <fcntl.h>
#include <error.h>
int match(char* s1, char* s2) {
int diff = strlen(s1) - strlen(s2);
if(strlen(s1) > strlen(s2)) return (strcmp(&s1[diff], s2) == 0);
else return 0;
}
int main(int argc, char* argv[]) {
DIR* info = NULL;
struct dirent* entry = NULL;
info = opendir(argv[1]);
if(info != NULL) {
while(entry = readdir(info)) {
if(match(entry->d_name, argv[2]))
printf("i-node : %ld, name : %s\n", entry->d_ino, entry->d_name);
}
closedir(info);
}
return 0;
}
컴파일 :
gcc dir.c
실행 :
예를 들어 아래 사진과 같이 디렉토리와 파일을 만들어놨을 때,
% ./a.out mydir txt
Comments