11 int main()
12 {
13 char path1[BUFSIZE];
14 ssize_t nread;
15 DIR *fp;
16 struct dirent *item;
17 int fdin;
18 int count = 0;
19 const char *path;
20
21 fp = opendir("/home/khs/week5_22");
22 if(fp != NULL){
23 for(;;)
24 {
25 item = readdir(fp);
26 path = readdir(fp); //권한 변경을 시도하려고 만든 부분
27
28 if (item != NULL){
29 chmod(path, 777); // 권한 변경을 시도한 부분.
30 printf("succed to change the mode");
31
32 }//else if (S_ISDIR(fp->st_mode)){
33 // printf("Fail to change the mode");
34 //}
35
36 if (item == NULL){
37 break;
38 }
39 printf("%s\t",item->d_name);
40 count +=1;
41 if(count%2 == 0)
42 printf("\n");
43 }
44 closedir(fp);
45 printf("\n");
46
47
48
49 }
50 }
//체크 한 부분에서 오류가 나거든여 chomd형식이 chmod(const char * , mode_t mode) 라고 해서 const char *path 만둘어서 받았는데 , path = readdir(fp);는 또 안되던군요 어떻게 해야하죠?
chmod(item->d_name, 0777);
만약에 스트링만 가지고 싶으면 strncpy 쓰셈
고마워요..
그리고 c에선 0이 앞에 붙을 때 8진수고 0x가 앞에 붙으면 16진수임.
어떡하긴 레퍼런스 봐야지.
https://pubs.opengroup.org/onlinepubs/009695399/functions/readdir_r.html
이게 돌려주는게, struct dirent * 인데.. 그걸 char * 에 넣으려니 에러나지. 질문하기 전에 레퍼런스 문서 보셈. 함수 쓰기 전에 함수 용법 모르면 레퍼런스 문서 보셈. 문서 안 보고 뇌피셜로 코드를 작성하니까.. 계속 에러나는거임
chmod()... 후에 printf ("success...") 하면.. 체크가 안 됨.
https://pubs.opengroup.org/onlinepubs/009695399/functions/chmod.html
Upon
successful completion, 0 shall be returned; otherwise, -1 shall be returned and errno set to indicate the error. If -1 is returned, no change to the file mode occurs.
그리고 에러 메시지 알려면 errno.h 어떻게 써먹는지 알아야 함.
아마 c 초보자 같은데, 코드 작성할 때 함수 쓰잖아.. 그거 일일이 레퍼런스 메뉴을 찾아서 인수(인자) 타입은 뭔지, 리턴 타입은 뭔지, 에러나면 뭘 돌려주는지, 문자열을 인수를 받는 함수의 경우 NULL 을 넣어도 되는지 일일이 하나씩 다 찾아서 읽어야 하는게 정석임.
무슨 말 하시는지 알거같아요.. 덕분에 제데로 고쳤슴돠
앞으론 념글에 질문법 읽고 와주세요... 코드를 그대로 올리면 읽기가 힘들어요