您的位置:首页 > 编程语言

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

2012-12-17 21:07 246 查看
/*
* 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

-csame as -t c, select ASCII characters or backslash escapes
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: