chdir 사용법
개요
현재 경로를 이동해주는 함수
사용법
1
2
3
|
#include <unistd.h>
int chdir(const char *path);
|
cs |
- 성공 시 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
|
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
void create_file()
{
int fd;
if ((fd = open("newfile",O_CREAT, 0644)) == -1)
{
perror("open failed");
exit(1);
}
close(fd);
}
int main(int argc, char *argv[])
{
if (argc != 1)
{
int result = chdir(argv[1]);
if (result == 0)
{
printf("success\n");
create_file();
}
else
printf("fail\n");
}
else
printf("Error : Please enter the path\n");
return (0);
}
|
cs |
만약 해당하는 경로가 없으면 fail
해당하는 경로가 있다면 해당 디렉터리에서 파일을 하나 생성함.
'프로그래밍 언어 > C, C++' 카테고리의 다른 글
[C언어] GNU readline 사용법 (1) | 2022.01.18 |
---|---|
[C/C++] atoi 구현 / 문자열을 숫자로 (11) | 2021.04.18 |
[C언어] 문자열을 숫자로, 숫자를 문자열로 (0) | 2021.01.27 |
[C언어] 배열, 구조체의 원소 개수 구하기 / sizeof 사용법 (0) | 2020.11.13 |
[C/C++] 최대공약수 구하기 (0) | 2020.10.31 |