unix环境高级编程 FILE I/O笔记

12 篇文章 0 订阅


/*  
 * 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\n");

        if(write(fd,buf1,10)!=10)
                printf("buf1 write error\n");
        /*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\n");
        /*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

每一行输出的前七位为八进制表示40000表示成10进制 (40000)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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值