C & LINUX
C / LINUX creat()
Ocean_
2022. 5. 1. 15:21
함수 기능
파일을 생성할 때 사용하는 시스템 호출
함수 원형
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
int create(const char*pathname, mode_t mode);
리턴 값 : 성공시 파일디스크립터, 에러시 -1
함수 파라메터
pathname
생성하고자 하는 파일 이름
mode
open의 모드와 동일
함수 예제
creat1
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
int main(void){
char *fname = "ssu_test.txt";
int fd;
if((fd = creat(fname,0666)<0)){
fprintf(stderr,"creat error for %s\n",fname);
exit(1);
}
else{
printf("Success!\nFilename : %s\nDescriptor : %d\n",fname,fd);
close(fd);
}
exit(0);
}
creat2
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
int main(void){
char *fname = "ssu_test.txt";
int fd;
if((fd = creat(fname,0666))<0){
fprintf(stderr,"creat error for %s\n",fname);
exit(1);
}
else{
close(fd);
fd = open(fname,O_RDWR);
printf("Succeeded!\n<%s> is new readable and writable\n",fname);
}
exit(0);
}
함수 결과
creat1
creat2
리눅스시스템 프로그래밍 - 홍지만 저
교재 내에 있는 예제를 바탕으로 작성한 글 입니다.