유닉스 환경 고급 프로그래밍 FILE I/O 노트

3542 단어
/*  
 * Default file access permissions for new files.  
*/    
 #define FILE_MODE   (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) 
FILE_MODE  apue.h       
     (owner)  、   ;                
S_IRUSR  header file  sys/stat.h
 S_IRUSR    00400     owner has read permission
           S_IWUSR    00200     owner has write permission
           S_IXUSR    00100     owner has execute permission
           S_IRWXG    00070     mask for group permissions
           S_IRGRP    00040     group has read permission
           S_IWGRP    00020     group has write permission
           S_IXGRP    00010     group has execute permission

           S_IRWXO    00007     mask for permissions for others (not in group)
           S_IROTH    00004     others have read permission
           S_IWOTH    00002     others have write permission
           S_IXOTH    00001     others have execute permission
#include<fcntl.h>
#include<sys/stat.h>
#include<stdio.h>
char buf1[]="abcdefghij";
char buf2[]="ABCDEFGHIJ";

#define FILE_MODE   (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)

int main(void)
{
        //file descriptor
        int fd;
        if((fd=creat("file.hole",FILE_MODE))<0)
                printf("create error
"); if(write(fd,buf1,10)!=10) printf("buf1 write error
"); /*offset now=10 */ if(lseek(fd,16384,SEEK_SET)==-1) printf("lseek error"); /*offset now =16384 */ if(write(fd,buf2,10)!=10) printf("buf2,write error
"); /*offset now =16394 */ exit(0); }

od -c file.hole 0000000   a   b   c   d   e   f   g   h   i   j  \0  \0  \0  \0  \0  \0 0000020  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0 * 0040000   A   B   C   D   E   F   G   H   I   J 0040012
한 줄당 출력의 7위는 8진법으로 40000을 표시하고 10진법으로 표시한다(4000)oct=4*(8^4)=2^14=16384
od command:
Write an unambiguous representation, octal bytes by default, of FILE to standard output. With more than one FILE argument, concatenate them in the listed order to form the input. With no FILE, or when FILE is -, read standard input
-c
same as 
-t c, select ASCII characters or backslash escapes

좋은 웹페이지 즐겨찾기