1.打开(open)
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
//int open(const char *pathname, int flags);
//int open(const char *pathname, int flags, mode_t mode);
//int creat(const char *pathname, mode_t mode);
int main(int argc, char *argv[])
{
int fd;
fd = open("./file1", O_RDWR);
printf("fd = %d\n", fd);
return 0;
}
2.读、写(write、read)
int main(int argc, char *argv[])
{
int fd;
int n_write;
int n_read;
char *buf = "zhangkun hen shuai!";
fd = open("./file1", O_RDWR);
printf("fd = %d\n", fd);
if (fd == -1){
printf("open file1 failed!\n");
fd = open("./file1", O_RDWR | O_CREAT, 0600);
if (fd > 0){
printf("open file1 successfully!\n");
}
}
printf("create success: fd = %d\n", fd);
// ssize_t write(int fd, const void *buf, size_t count);
n_write = write(fd, buf, strlen(buf)); //注意这里如果用sizeof(buf),写进去的字符串只有8个字节,并不是buf的真实长度
if (n_write != -1){
printf("write successfully!\n");
}
char *readBuf = NULL;
readBuf = (char *)malloc(sizeof(char)*n_write + 1);
n_read = read(fd, readBuf, n_write);
printf("read :%d, context :%s\n", n_read, readBuf);
return 0;
}
~
3.光标定位
printf("create success: fd = %d\n", fd);
// ssize_t write(int fd, const void *buf, size_t count);
n_write = write(fd, buf, strlen(buf)); //注意这里如果用sizeof(buf),写进去的字符串只有8个字节,并不是buf的真实长度
if (n_write != -1){
printf("write successfully!\n");
}
char *readBuf = NULL;
readBuf = (char *)malloc(sizeof(char)*n_write + 1);
// off_t lseek(int fd, off_t offset, int whence);
lseek(fd, 0, SEEK_SET); //第二个参数为光标位置的偏移值,为正的就是向后偏移,为负则往前偏移,第三个参数则是光标位置(set,end,cur)
n_read = read(fd, readBuf, n_write);
printf("read :%d, context :%s\n", n_read, readBuf);
return 0;
注意lseek函数有三个参数,最后一个参数包含SEKK_SET、SEKK_END、SEKK_CUR,分别代表光标在开头、末尾、当前位置,使用该API可以避免光标在末尾,读不到内容。同时,lseek的返回值还可以计算整个文件数据内容大小:
int main(int argc, char *argv[])
{
int fd;
int file_size;
fd = open("./file1", O_RDWR);
printf("fd = %d\n", fd);
// off_t lseek(int fd, off_t offset, int whence);
//file_size = lseek(fd, 19, SEEK_SET);
file_size = lseek(fd, 0, SEEK_END); //lseek返回值可以计算文件数据字节数
printf("the size of file1 is %d\n", file_size);
close(fd);
return 0;
}
4.O_EXCL
int main(int argc, char *argv[])
{
int fd;
char *buf = "zhangkun hen shuai!";
fd = open("./file1", O_RDWR | O_CREAT | O_EXCL, 0600);
printf("fd = %d\n", fd);
if (fd == -1){
printf("file1 has already exist!\n");
}
write(fd, buf, strlen(buf)); //注意这里如果用sizeof(buf),写进去的字符串只有8个字节,并不是buf的真实长度
close(fd);
return 0;
}
5.O_APPEND
int main(int argc, char *argv[])
{
int fd;
char *buf = "zhangkun hen shuai!";
fd = open("./file1", O_RDWR | O_CREAT | O_APPEND, 0600); //O_APPEND每次写时会将数据加在文件末尾,不加O_APPEND则会覆盖先前的内容
printf("fd = %d\n", fd);
// ssize_t write(int fd, const void *buf, size_t count);
write(fd, buf, strlen(buf)); //注意这里如果用sizeof(buf),写进去的字符串只有8个字节,并不是buf的真实长度
close(fd);
return 0;
}
6.O_TRUNC
int main(int argc, char *argv[])
{
int fd;
char *buf = "over";
fd = open("./file1", O_RDWR | O_CREAT |O_TRUNC, 0600); //O_TRUNC打开文件会直接抹去文件原来所有数据内容
printf("fd = %d\n", fd);
// ssize_t write(int fd, const void *buf, size_t count);
write(fd, buf, strlen(buf)); //注意这里如果用sizeof(buf),写进去的字符串只有8个字节,并不是buf的真实长度
close(fd);
return 0;
}
7.创建(creat)
int main(int argc, char *argv[])
{
int fd;
char *buf = "hhhhha";
fd = creat("./file2", S_IRWXU); //creat()返回值也是一个fd
printf("fd = %d\n", fd);
// ssize_t write(int fd, const void *buf, size_t count);
write(fd, buf, strlen(buf)); //注意这里如果用sizeof(buf),写进去的字符串只有8个字节,并不是buf的真实长度
close(fd);
return 0;
}
总结:
Pathname:要打开的文件名(含路径,缺省为当前路径)
Flags: O_RDONLY 只读打开 O_WRONLY 只写打开 O_RDWR 可读可写打开
当我们附带了权限后,打开的文件就只能按照这种权限来操作。以上这三个常数中应当只指定一 个。下列常数是可选择的:
O_CREAT 若文件不存在则创建它。使用此选项时,需要同时说明第三个参数mode,用其说明该新文件的存取许可权限。
O_EXCL 如果同时指定了OCREAT,而文件已经存在,则出错。
O_APPEND 每次写时都加到文件的尾端。
O_TRUNC 属性去打开文件时,如果这个文件中本来是有内容的,而且为只读或只写成功打开,则将其长度截短为0。
Mode:一定是在flags中使用了O_CREAT标志,mode记录待创建的文件的访问权限