unix c programming-1
글쓴이: smalljam / 작성시간: 일, 2003/03/23 - 3:14오전
유닉스에서 모든 것은 화일입니다.
그러면 화일과의 첫만남은 화일 오픈입니다.
#include <fcntl.h>
main(){
char *fname="test.txt";
int fd;
fd=open(fname,O_RDONLY);
if(fd>0){
printf("Sucecc!\nFilename:%s,descriptor:%d\n",fname,fd);
}else{
printf("Faild!\n");
}
exit(0);
}
Forums:


유닉스의 모든것은 화일입니다.
열려진 화일을 읽어야겠지요
#include <fcntl.h> main(){ int fd; char c; if((fd=open("test.txt",O_RDONLY))<0){ printf("<test.txt> cannot be opened!\n"); exit(1); } for(;;){ if(read(fd,&c,1)>0) putchar(c); else break; } exit(0); } 다음에는 화일에 쓰기입니다.In the UNIX,
화일 시스템은 지평적인 공간 감각을 제공하며 ,
프로세스는 생명을 갖는 생명체와 같아보인다.
--BACH
화일과의 첫만남은 화일을 읽고 쓰기입니다.읽기위해서,화일 생
화일과의 첫만남은 화일을 읽고 쓰기입니다.읽기위해서,화일 생성을 해야합니다.화일 open과 거의 흡사합니다.
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> main(){ char *fname="test.txt"; int fd; if((fd=creat(fname,0644))>0){ close(fd); fd=open(fname,O_RDWR); printf("Sucess!\n<%s> is now readable and writable.\n",fname); } else printf("faile!\n"); exit(0); }In the UNIX,
화일 시스템은 지평적인 공간 감각을 제공하며 ,
프로세스는 생명을 갖는 생명체와 같아보인다.
--BACH
화일을 control하는것은 몇가지 있습니다.그중 하나인 lseek입니
화일을 control하는것은 몇가지 있습니다.그중 하나인 lseek입니다.
#include <fcntl.h> #include <sys/types.h> main(){ char *fname="test.txt"; int fd; off_t fsize; if((fd=open(fname,O_RDONLY))<0){ printf("<%s> cannot be opened!",fname); exit(-1); } fsize=lseek(fd,0,SEEK_END); printf("The size of <%s> is %lu bytes.\n",fname,fsize); exit(0); }In the UNIX,
화일 시스템은 지평적인 공간 감각을 제공하며 ,
프로세스는 생명을 갖는 생명체와 같아보인다.
--BACH
화일을 control하는데 두번째 함수인 fcntl입니다.
화일을 control하는데 두번째 함수인 fcntl입니다.
#include <fcntl.h> #include <sys/types.h> #include <unistd.h> main(){ char *fname="test.txt"; int fd,newfd,flag; fd=open(fname,O_RDONLY|O_APPEND,0644); fcntl(fd,F_SETFD,FD_CLOSEEC); flag=fcntl(fd,F_FGETFL,0); if(flag&O_APPEND) printf("fd:O_APPEND flag is set\n"); else pritf("fd:O_APPEND flag is NOT set\n"); flag=fcntl(fd,F_GETFD,0); if(flag&FD_CLOSEEC) printf("fd:FD_CLOSEEC flag is set\n"); else printf("fd:FD_CLOSEEC flag is NOT set\n"); newfd=fcntl(fd,F_DUPFD,0); flag=fcntl(newfd,F_GETFL,0); if(flag&O_APPEND) printf("newfd:O_APPEND flag is set\n"); else printf("newfd:O_APPEND flag is NOT set\n"); if(flag &FD_CLOSEEC) printf("newfd:FD_CLOSEEC flag is set\n"); else printf("newfd:FD_CLOSEEC flag is NOT set\n"); close(0); }In the UNIX,
화일 시스템은 지평적인 공간 감각을 제공하며 ,
프로세스는 생명을 갖는 생명체와 같아보인다.
--BACH