파일명/디렉토리명 변경

#include <unistd.h>

int rename(const char *oldname, const char *newname);

파일이나 디렉토리의 이름을 변경, 성공하면 0, 실패하면 -1

1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
 
int main(int argc, char **argv)
{
    if(rename(argv[1],argv[2])<0)
        perror("error : ");
 
    else
        printf("success\n");
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

 

디렉토리 관련 함수

현재 작업 디렉토리 알아보기

#include <unistd.h>

char *getcwd (char *buf, size_t size);

현재 작업 디렉토리를 얻는다, 성공하면 buf, 실패하면 NULL

각 프로세스 마다 자신의 작업 디렉토리가 따로 존재

 

현재 작업 디렉토리 바꾸기

#include <unistd.h>

int chdir (const char *pathname);

int fchdir (int filedes);

현재 작업 디렉토리를 변경, 성공하면 0 실패하면 -1

프로세스가 변경한 현재 작업 디렉토리의 값은 부모 프레섯의 현재 작업 디렉토리에는 영향을 주지 않는다.

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
#include <linux/limits.h>
#include <unistd.h>
#include <stdio.h>
 
int main(int argc, char **argv)
{
    if(argc!=2)
    {
        fprintf(stderr, "Usage: %s <path name>\n",argv[0]);
        return 1;
    }
 
    char name[PATH_MAX];
    printf("Before Current Directory:%s\n", getcwd(name, PATH_MAX));
    if(chdir(argv[1])==-1)
    {
        printf("failed,change directory\n");
    }
    else
    {
        printf("After Current Directory:%s\n",getcwd(name,PATH_MAX));
    }
    return 0;
}
 
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

 

 

디렉토리 접근

#include <sys/types.h>

#include <dirent.h>

DIR *opendir (const char *pathname);

struct dirent *readdir (DIR *dp);

opendir() 성공하면 해당 포인터, 실패하면 NULL

readdir() 수행 시 마다 디렉토리 파일의 오프셋은 sizeof(struct dirent) 만큼 증가

long telldir (DIR *dp);

void seekdir (DIR *dp, long pos);

telldir() 현재 디렉토리 파일안의 오프셋을 반환

seekdir() - readdir()호출이 시작할 디렉토리 스트림의 위치를 설정, telldir()에 반환된 offset 값을 사용

void rewinddir (DIR *dp);

int closedir (DIR *dp);

rewinddir() 디렉토리 파일의 오프셋을 처음으로 변경

closedir() 디렉토리를 닫는다. 성공하면 0 실패하면 -1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <dirent.h>
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
 
int main()
{
    struct dirent *item;
    DIR *dp;
 
    dp = opendir("/");
 
    if(dp!=NULL){
        for(;;){
            item = readdir(dp);
            if(item==NULLbreak;
            printf("DIR : %s\n", item -> d_name);
        }
        closedir(dp);
    }
}
 
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

 

 

특수 목적 파일 연산

메모리로 사상된 파일

Memory Mapped I/O

입출력 함수를 사용하지 않고, 메모리 접근만으로 파일 입출력 가능

#include <sys/types.h>

#include <sys/mman.h>

caddr_t mmap(caddr_t addr, size_t len, int prot, int flag, int filedes, off_t off);

호출 성공시 매핑된 부분의 시작 주소를 반환, 에러 시 -1

int munmap(caddr_t addr, size_t len);

호출 성공시 0, 에러 시 -1

 

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <string.h>   
#ifndef MAP_FILE
#define MAP_FILE 0
#endif
 
int main(int argc, char *argv[])
{   
    int ifd,ofd;
    char *ibuf,*obuf;
    struct stat statbuf;
 
    if (argc!=3){
        perror("usage: %s sourcefile targetfile"); return -1;
    }   
 
    if((ifd=open(argv[1],O_RDONLY))<0)
    {   
        perror("open error");return -1;
    }   
    if((ofd=open(argv[2],O_RDWR|O_CREAT|O_TRUNC,0644))<0)
    {   perror("open error"); return -1
    }
 
    if(fstat(ifd,&statbuf)<0//sourcefile 크기 정보 필요
    {
        perror("fstat error"); return -1;
    }
 
    //출력 파일의 크기를 미리 설정
    if(lseek(ofd,statbuf.st_size -1,SEEK_SET)<0)
    {    perror("lseek error"); return -1;}
    if(write(ofd,"",1)!=1)
    { perror("write error"); return -1;}
 
    //sourcefile 메모리 매핑
 
    if((ibuf=mmap(0,statbuf.st_size, PROT_READ, MAP_FILE|MAP_SHARED,ifd,0))<0)
    {
        perror("mmap error"); return -1;
    }
 
    //targetfile 메모리 매핑
    if((obuf=mmap(0,statbuf.st_size, PROT_READ|PROT_WRITE,
                    MAP_FILE|MAP_SHARED,ofd,0))<0)
    {
        perror("mmap error"); return -1;}
 
    //파일 전체 복사
    memcpy(obuf,ibuf,statbuf.st_size);
 
    return 0;
}
 
 
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

 

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
33
34
35
36
37
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
 
int main(int argc,char **argv)
{
    int fd;
    struct stat st;
    caddr_t base, ptr;
    
    while(--argc){
        if((fd=open(*++argv,O_RDONLY,0))<0){
            perror(*argv);
            continue;
        }
        fstat(fd,&st);
 
        base=mmap(0,st.st_size,PROT_READ, MAP_SHARED,fd,0);
 
        if(base==MAP_FAILED){
            perror(*argv);
            close(fd);
            continue;
        }
 
        close(fd);
 
        for(ptr=base;ptr<&base[st.st_size];ptr++)putchar(*ptr);
        munmap(base,st.st_size);
    }
    return 0;
}
 
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

 

+ Recent posts