1 / 7

예제 2-3 2006242018 김슬기

예제 2-3 2006242018 김슬기. main( argc,argv ) int argc ; char * argv []; { int fd , open(), getpid (); struct employee record; if( argc &lt; 2) { fprintf(stderr,&quot;Usage: %s file<br>&quot;,argv[0]); exit(1); } if(( fd = open( argv [1],O_WRONLY | O_CREAT | O_EXCL, 0640))== -1) {

aqua
Download Presentation

예제 2-3 2006242018 김슬기

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 예제 2-32006242018 김슬기

  2. main(argc,argv) intargc; char *argv[]; { intfd, open(), getpid(); struct employee record; if(argc < 2) { fprintf(stderr,"Usage: %s file\n",argv[0]); exit(1); } if((fd= open(argv[1],O_WRONLY | O_CREAT | O_EXCL, 0640))== -1) { perror(argv[1]); exit(2); } 변수및 함수 구조체 선언

  3. for(;;) { printf("Enter employee name <SPACE> salary: "); scanf("%s",record.name); if(record.name[0] == '.') break; scanf("%d",&record.salary); record.pid= getpid(); write(fd,(char *) &record, sizeof(record)); } close(fd); exit(0); } “.” 가 오지 않을 때까지 루프를 돌면서 구조체 record 의 name 과 salary 를 입력 받고 Getpid() 함수를 이용해 pid에 프로세스 id를 반환하여 대입하고 파일에 write

  4. 실행화면

  5. 함수설명 main(argc,argv) Argc : argument count 인자의 개수 Argv : argument vector 인자의 벡터 Ex) #Program arg1 arg2 arg3 argc : 실행파일 이름과 인자 3을 합쳐 4가된다. argv : argv[0] – “Program\0”argv[1] – ”argv1\0” argv[2] – “argv2\0” argv[3] – “argv3\0”

  6. 함수설명 Stderr 에러를 표시할때 사용하며 fprintf() 인 스트림에 출력하는 함수에 사용 Perror 오류 메세지를 출력한다.전역 변수 errno의 값을 해석하여 이에 해당하는 시스템 오류 메세지를 표준 오류 출력 스트림에 출력한다. O_WRONLY|O_CREAT| O_EXCL flag로서 파일을 열때 어떤 모드로 열 것인지 결정하는 것입니다. O_WRONLY : 쓰기 전용으로 열다 O_CREAT : 파일이 존재하지 않으면 파일 생성 #include<fcntl.h>에 포함

  7. 함수설명 Getpid() 프로세스의 id를 리턴 Open() 파일을 열때 사용되며순서대로 첫번째 인자는 파일명(경로)이고 두번째 인자는 용도이며, 세번째는 허가사항이다. Write() 파일을 쓸때 사용되며 첫번째 인자는 파일명(경로)이고 두번째 인수에 주소연산자 &가 붙은것은 write() 함수의 두번째 인수가 버퍼의 주소를 넘겨받기 때문이다.

More Related