
00007
others have read, write and execute permission
S_IROTH
00004
others have read permission
S_IWOTH
00002
others have write permission
S_IXOTH
00001
others have execute permission
例:
fd = creat(argv[i], 0664);

其中 0664 表示的是该文件:
/*open*/
*************************************************************************
> 头文件 -> #include <sys/types.h>
-> #include <sys/stat.h>
-> #include <fcntl.h>
> 文件存在 -> 正常打开文件
> int open(const char *pathname, int flags);
> 文件不存在 -> 则创建并打开
> int open(const char *pathname, int flags, mode_t mode);
> int open(文件路径名,文件打开方式,文件访问权限);
> 参数一 -> pathname -> 创建文件的路径
> 参数二 -> flags -> 打开文件的方式
> 参数三 -> mode -> 指定创建文件的访问权限
> 返回值(成功) -> 返回打开文件的描述符
> 返回值(失败) -> 出错返回-1并设置errno
> 作用 -> 打开文件
*************************************************************************
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
int main(int argc, char *argv[]){
int fd = -1;
// O_CREAT:若文件不存在则按照指定的权限创建文件
// 若文件存在则没有影响
// fd = open(argv[1], O_RDWR|O_CREAT, 0664);
fd = open(argv[1], O_RDWR |O_CREAT
, S_IWUSR|S_IRUSR|S_IWGRP|S_IRGRP|S_IROTH);
if (-1 == fd){
perror("open file");
}else{
printf("open %s ok, fd = %d\n", argv[1], fd);
// 关闭文件
close(fd);
}
return 0;
}
运行结果:
$ ./a a
open file: Text file busy
$ ./a b
open b ok, fd = 3
$
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
int main(int argc, char *argv[]){
int fd = -1;
// O_RDONLY:以只读的方式打开文件 -> 若文件不存在,则不会自动创建文件
// fd = open(argv[1], O_RDONLY);
// O_WRONLY:以只写的方式打开文件 -> 若文件不存在,则不会自动创建文件
// fd = open(argv[1], O_WRONLY);
// O_RDWR:以读与写的方式打开文件 -> 若文件不存在,则不会自动创建文件
fd = open(argv[1], O_RDWR);
if (-1 == fd){
perror("open file");
}else{
printf("open %s ok, fd = %d\n", argv[1], fd);
// 关闭文件
close(fd);
}
return 0;
}
运行结果:
$ ./a a
open file: Text file busy
$ ./a b
open b ok, fd = 3
$ ./a c
open file: No such file or directory
$
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
int main(int argc, char *argv[]){
int fd = -1;
// O_TRUNC:若文件为一个常规文件
// 并且是写的方式打开
// 则会将文件的内容清除
fd = open(argv[1], O_RDWR|O_TRUNC);
if (-1 == fd){
perror("open file");
}else{
printf("open %s ok, fd = %d\n", argv[1], fd);
// 关闭文件
close(fd);
}
return 0;
}
运行结果:
$ ./a a
open file: Text file busy
$ ./a b
open b ok, fd = 3
$ ./a c
open file: No such file or directory
$
MUSTCANMORE
O_RDONLY | O_WRONLY | O_RDWR
O_CREAT | O_TRUNC | O_EXCL | O_NOCTTY

O_APPEND
宏详细说明
O_RDONLY
以只读的方法开启文件 -> 若文件不存在,则不会自动创建文件
O_WRONLY
以只写的方法开启文件 -> 若文件不存在python s ixusr,则不会自动创建文件
O_RDWR
以读写的方法开启文件 -> 若文件不存在,则不会自动创建文件
O_TRUNC
打开文件时清空内容
O_CREAT
没有文件时创建文件
O_APPEND

每次从文件末尾写入内容
/*write*/
*************************************************************************
> 头文件 -> #include <unistd.h>
> ssize_t write(int fd, const void *buf, size_t count);
> ssize_t write(文件描述符,写入数据的地址,写入数据的长度);
> 参数一 -> fd -> 文件描述符
> 参数二 -> buf -> 写入数据的地址
> 参数三 -> count -> 写入数据的长度
> 返回值(成功) -> 实际写入的字节数
> 返回值(失败) -> 出错返回-1并设置errno
> 作用 -> 写入文件
*************************************************************************
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
int main(int argc, char *argv[]){
char *pMsg = "我爱C语言\n";
// printf("%s\n", pMsg);
// 标准输出文件系统默认打开,可以直接进行写操作
// printf("%d\n", STDOUT_FILENO);
// STDOUT_FILENO : 1
write(STDOUT_FILENO, pMsg, strlen(pMsg));
return 0;
}
运行结果:
$ ./a
我爱C语言
$
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
int main(int argc, char *argv[]){
int fd = -1;
fd = open(argv[1], O_WRONLY|O_CREAT|O_APPEND, 0664);
if (-1 == fd){
perror("open file");
}else{
printf("open %s ok, fd = %d\n", argv[1], fd);
char caBuf[1024] = {'\0'};
char enTer[8]="\n";
ssize_t ret = -1;
while (1){
printf("请输入数据:\n");
scanf("%s", caBuf);
if (0 == strcmp("exit", caBuf)){
break;
}
// 将caBuf中的数据写入fd表示的文件中去
// 成功返回写入文件的字节数
// 失败返回-1
ret = write(fd, caBuf, strlen(caBuf));
write(fd, enTer, strlen(enTer));
if (-1 == ret){
perror("write");
break;
}
printf("write %d bytes to %s\n", ret, argv[1]);
}
// 关闭文件
close(fd);
}
return 0;
}
运行结果:
$ ./a
open file: Bad address
$ ./a a
open file: Text file busy
$ ./a b
open b ok, fd = 3
请输入数据:
HelloWorld!
write 11 bytes to b
请输入数据:
exit
$ cat b
HelloWorld!
$
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
typedef struct STU{
int iId;
char caName[32];
float fScore;
}STU;
int main(int argc, char *argv[]){
int fd = -1;
fd = open(argv[1], O_WRONLY|O_CREAT, 0664);
if (-1 == fd){
perror("open file");
}else{
STU stu[3] = {{1001, "jack", 90}
,{1002, "rose", 89}
,{1003, "tom" , 93}};
/* write(fd, stu, sizeof(stu)); -> same as follow */
int i = 0;
ssize_t ret = -1;
for (; i < 3; i++){
ret = write(fd, stu+i, sizeof(STU));
if (ret > 0){
printf("write %d bytes to %s\n", ret, argv[1]);
}
}
// 关闭文件
close(fd);
}
return 0;
}
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/shumachanpin/article-121793-1.html
美狗再不收敛
不让别的国家自由的美国
1000次911美国消失得差不多了