前言:从这个博文开始我们开始学习Linux相关的编程,探索Linux奥秘
1.第一个API函数是open函数
open有两种表现形式
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
open函数返回的是一个对应的文件描述符
pathname:要打开的文件名(含路径,缺省为当前路径)
flags:以什么的方式打开,O_RDONLY(只读打开),O_WRONLY(只写打开),O_RDWR(可读可写打开)
mode:打开方式的数字表现形式,例如,0600是代表可读可写的意思,切记mode一定是在flags中
使用了O_CREAT标志才能用,mode记录待创建的文件访问权限
上代码:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
/*int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);*/
int fd;
fd = open("./file1",O_RDWR);
if(fd == -1)
{
printf("failded to open file1!\n");
fd = open("./file1",O_RDWR|O_CREAT,0600);
if(fd > 0)
{
printf("Success to open file1!\n");
}
}
printf("fd = %d\n",fd);
return 0;
}
切记:O_RDWR(可读可写),O_WRONLY(只写打开),O_RDONLY(只读打开)
当附带上上述权限后,打开的文件只能按照这种权限来操作,以上的三个常数中应当只指定一个,而下面的这些常数是可选择的。
(1)O_CREAT:若文件不存在,则创建它,使用此选项时,需要同时说明第三个参数mode,用其说 明该新文件的存取许可权限。
(2)O_EXCL:如果同时指定了O_CREAT,而文件已经存在的,则会出错(即open会返回个-1),注意,如果有O_EXCL的存在的话,write函数是写不进东西的。
(3)O_APPEND:每次写入文件时都加到文件的尾端,即写入文件时,如果加到O_APPEND的话, write会根据文件里的原有内容将新内容接到旧内容尾巴去,如果没加O_APPEND,则会根据新内容的长度覆盖旧内容的对应的长度。
(4)O_TRUNC:以这个属性去打开文件时,如果这个文件本来是有内容的,而且只为只读或只写成功打开后,则将其长度截断为0,其实说白了就是将文件的旧内容全部删掉,写入新的内容。
上代码:
(1)O_EXCL和O_CREAT:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int fd;
fd = open("./file3",O_RDWR|O_CREAT|O_EXCL,0600);
printf("fd = %d\n",fd);
close(fd);
return 0;
}
这时如果file3存在的话,则fd = -1,如果没存在则返回一个大于0的数
(2)O_APPEND:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int fd;
int n_write;
char *writeBuf = "hello world!!!";
fd = open("./file3",O_RDWR|O_CREAT|O_APPEND,0600);
printf("fd = %d\n",fd);
n_write = write(fd,writeBuf,strlen(writeBuf));
printf("n_write = %d\n",n_write);
close(fd);
return 0;
}
我们知道如果不加O_APPEND的话,新内容会覆盖旧内容,加了就在尾巴接上去。
(3)O_TRUNC
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int fd;
int fd1;
int n_write;
char *writeBuf = "tests";
fd = open("./file3",O_RDWR|O_CREAT|O_TRUNC,0600);
printf("fd = %d\n",fd);
n_write = write(fd,writeBuf,strlen(writeBuf));
printf("n_write = %d\n",n_write);
//int creat(const char *pathname, mode_t mode);
fd1 = creat("./file4",S_IRWXU);
printf("fd1 = %d\n",fd1);
close(fd);
return 0;
}
这里O_TRUNC会将旧内容全部清除掉。
2.第2个API函数是write函数
ssize_t write(int fd, const void *buf, size_t count);
write函数返回的是写入到文件里的字节数
意思是从字符串buf中读取count个字节写到fd文件中。
上代码:
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main()
{
//ssize_t write(int fd, const void *buf, size_t count);
int fd;
char *buf = "Hello world!";
fd = open("./file1",O_RDWR);
printf("fd = %d\n",fd);
if(fd == -1)
{
printf("fail to open the file1!\n");
fd = open("./file1",O_RDWR|O_CREAT,0600);
if(fd > 0)
{
puts("Success to open the file1!");
}
}
printf("fd = %d\n",fd);
write(fd,buf,sizeof(buf));
write(fd,buf,strlen(buf));
close(fd);
return 0;
}
将字符串buf写入到fd所指向的文件里去,记得计算长度时要用strlen而不是sizeof,sizeof计算出的是指针buf的大小,strlen计算的才是buf内容的有效长度。
学习笔记,仅供参考