int open(const char* path, int access, [,unsigned mode]);
path : open file
access : file access mode setting
mode : new file mode
header file : io.h
path로 지정된 파일을 연다. 파일 입출력을 위한 준비를 하고 핸들을 발급한다.
액서스 모드는 읽을 것인지 쓸 것인지 새로 만들 것인지를 지정한다.
--------------------------------------------------
O_RDONLY : 읽기전용
O_WRONLY : 쓰기전용
O_RDWR : 읽고 쓰기
--------------------------------------------------
셋중 한개만 사용 해야 한다.
다음 아래는 선택적으로 필요시 사용하면된다.
--------------------------------------------------
O_NDELAY : 유닉스와 호환성 때문에 사용 X
O_APPEND : 파일 개방과 동시에 FP를 파일의 끝으로 이동 시켜 파일에 내용을 추가할수 있게 한다.
O_CREAT : 파일을 새로 만들어서 오픈 존재시 무시
O_EXCL : O_CREAT와 같이 사용 이미 존재할 경우는 에러 발생화 리턴
O_TRUNC : 파일이 이미 존재하고 있다면 파일의 길이를 0으로 만든다 즉 다시 만든다.
O_BINARY : 이진모드로 개방
O_TEXT : 텍스트 파일 모드로 개방
--------------------------------------------------
세번재 인수
--------------------------------------------------
S_IWRITE : 파일에 쓰기를 허용한다.
S_IREAD : 파일에 읽기를 허용한다.
S_IREAD | S_IWRITE : 파일에 읽기 쓰기를 허용한다.
--------------------------------------------------
에러 종류
--------------------------------------------------
ENOENT : 파일이나 디렉토리 지정 잘못
EMFILE : 너무 많은 파일 오픈
EACCESS : 파일의 접근이 금지됨
EINVACC : ACCESSS MODE 인수가 무효이다.
예제
----------------------------------------------------
#include <stdio.h>
#include <io.h>
#include <fcnt1.h>
#include <alloc.h>
#include <stdlib.h>
void main(int argc, char* argv[])
{
int handle;
int offset = 0;
int length;
void *buf;
if(argc < 2)
{
printf("I need parameter");
exit(1);
}
buf = malloc(60000);
if(buf == NULL)
{
printf("out of memory");
exit(1);
}
handle = open(argv[1],O_TEXT | O_RDONLY);
if(handle == -1)
{
printf("open faild");
exit(1);
}
length = read(handle, buf, 60000);
if(length == -1)
{
printf("reading error");
exit(1);
}
for(offset = 0; offset < length ; offset++)
putchar(((char*)buf)[offset];
close(handle);
free(buf);
}
다음 예제는 핸들 방식으로 파일을 복사한다
#include <stdio.h>
#include <stdlib.h>
#include <io.h>
#include <sys/stat.h>
#include <fcnt1.h>
#include <alloc.h>
void main(int argc, char* argv[])
{
int handle,handled,readnum;
void *buf;
if(argc < 2)
{
printf("I need two parameter");
exit(1);
}
buf = malloc(60000);
if(buf == NULL)
{
printf("out of memory");
exit(1);
}
handle = open(argv[1],O_BINARY | O_RDONLY);
if(handle == -1)
{
printf("open faild");
exit(1);
}
handled = open(argv[2],O_CREAT | O_BINARY, S_IWRITE);
if(handle == -1)
{
printf("open faild");
exit(1);
}
while((readnum = read(handles, buf, 60000)) != 0 && readnum != -1)
write(handled, buf, readnum);
close(handle);
close(handled);
free(buf);
}
path : open file
access : file access mode setting
mode : new file mode
header file : io.h
path로 지정된 파일을 연다. 파일 입출력을 위한 준비를 하고 핸들을 발급한다.
액서스 모드는 읽을 것인지 쓸 것인지 새로 만들 것인지를 지정한다.
--------------------------------------------------
O_RDONLY : 읽기전용
O_WRONLY : 쓰기전용
O_RDWR : 읽고 쓰기
--------------------------------------------------
셋중 한개만 사용 해야 한다.
다음 아래는 선택적으로 필요시 사용하면된다.
--------------------------------------------------
O_NDELAY : 유닉스와 호환성 때문에 사용 X
O_APPEND : 파일 개방과 동시에 FP를 파일의 끝으로 이동 시켜 파일에 내용을 추가할수 있게 한다.
O_CREAT : 파일을 새로 만들어서 오픈 존재시 무시
O_EXCL : O_CREAT와 같이 사용 이미 존재할 경우는 에러 발생화 리턴
O_TRUNC : 파일이 이미 존재하고 있다면 파일의 길이를 0으로 만든다 즉 다시 만든다.
O_BINARY : 이진모드로 개방
O_TEXT : 텍스트 파일 모드로 개방
--------------------------------------------------
세번재 인수
--------------------------------------------------
S_IWRITE : 파일에 쓰기를 허용한다.
S_IREAD : 파일에 읽기를 허용한다.
S_IREAD | S_IWRITE : 파일에 읽기 쓰기를 허용한다.
--------------------------------------------------
에러 종류
--------------------------------------------------
ENOENT : 파일이나 디렉토리 지정 잘못
EMFILE : 너무 많은 파일 오픈
EACCESS : 파일의 접근이 금지됨
EINVACC : ACCESSS MODE 인수가 무효이다.
예제
----------------------------------------------------
#include <stdio.h>
#include <io.h>
#include <fcnt1.h>
#include <alloc.h>
#include <stdlib.h>
void main(int argc, char* argv[])
{
int handle;
int offset = 0;
int length;
void *buf;
if(argc < 2)
{
printf("I need parameter");
exit(1);
}
buf = malloc(60000);
if(buf == NULL)
{
printf("out of memory");
exit(1);
}
handle = open(argv[1],O_TEXT | O_RDONLY);
if(handle == -1)
{
printf("open faild");
exit(1);
}
length = read(handle, buf, 60000);
if(length == -1)
{
printf("reading error");
exit(1);
}
for(offset = 0; offset < length ; offset++)
putchar(((char*)buf)[offset];
close(handle);
free(buf);
}
다음 예제는 핸들 방식으로 파일을 복사한다
#include <stdio.h>
#include <stdlib.h>
#include <io.h>
#include <sys/stat.h>
#include <fcnt1.h>
#include <alloc.h>
void main(int argc, char* argv[])
{
int handle,handled,readnum;
void *buf;
if(argc < 2)
{
printf("I need two parameter");
exit(1);
}
buf = malloc(60000);
if(buf == NULL)
{
printf("out of memory");
exit(1);
}
handle = open(argv[1],O_BINARY | O_RDONLY);
if(handle == -1)
{
printf("open faild");
exit(1);
}
handled = open(argv[2],O_CREAT | O_BINARY, S_IWRITE);
if(handle == -1)
{
printf("open faild");
exit(1);
}
while((readnum = read(handles, buf, 60000)) != 0 && readnum != -1)
write(handled, buf, readnum);
close(handle);
close(handled);
free(buf);
}
'Native > C' 카테고리의 다른 글
Master Tree (0) | 2013.10.02 |
---|---|
Heap sort (0) | 2013.10.02 |
Tree swap (0) | 2013.10.02 |
Doubl Linked list (0) | 2013.10.02 |
[과제] selective_sort 2 (0) | 2013.10.02 |