文件基础控制
文件操作
打开、写入、读取、关闭、光标
open:打开文件
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
pathname:需要打开文件的路径及名称的字符串,可以是相对路径,可以是绝对路径
flags:打开方式,而当打开附带上权限后,就只能按其附带的权限操作,可以传入多个参数选项
flags分为两类:主类,副类
主类:
O_RDONLY —只读打开
Q_WRONLY —只写打开
O_RDWR —可读、可写打开
这三个变量只能指定一个,这三个是互斥的
副类:
O_CREAT —若文件不存在,则创建它。需要使用mode(文件权限标志,0600)选项,来指明新文件的访问权限
O_EXCL —如果同时指定了O_CREAT,而打开文件存在,则出错
O_APPEND —每次写入文件时都把写入的内容追加到文件内容的尾端
O_TRUNC —打开文件时,如果这个文件本身是有内容的话,而且为只读或只写打开,则将其文件长度截短为0
主副可以配合使用,例如:O_RDWR|O_CREAT|O_TRUNC
mode:一定是flags中使用了O_CREAT,记录待创建的文件的访问权限,0600
- 说明:
R表示文件有读权限;
W表示文件有写权限;
X表示文件有执行权限;
U表示文件所属用户;
G表示文件所属用户所在组的用户;
O表示其他用户。
权限
可读 r 4
可写 w 2
可执行 x 1
0600:
0:
6:4+2 表示可读,可写权限
0:同组用户
0:其它组用户
————————————————
mode权限说明
1)文件所属用户对文件的访问权限1:
#define S_IRWXU 00700 表示文件所属用户,对文件有读写和执行权限
#define S_IRUSR 00400 表示文件所属用户,对文件有读权限
#define S_IWUSR 00200 表示文件所属用户,对文件有写权限
#define S_IXUSR 00100 表示文件所属用户,对文件有执行权限
2)文件所属用户所在组用户对文件的访问权限:
#define S_IRWXG 00070 表示对文件有读写和执行权限
#define S_IRGRP 00040 表示对文件有读权限
#define S_IWGRP 00020 表示对文件有写权限
#define S_IXGRP 00010 表示对文件有执行权限
3)其他用户对文件的访问权限:
#define S_IRWXO 00007 表示对文件有读写和执行权限
#define S_IROTH 00004 表示对文件有读权限
#define S_IWOTH 00002 表示对文件有写权限
#define S_IXOTH 00001 表示对文件有执行权限
————————————————
-
返回值:成功则返回打开文件的描述符(通常为3),失败则返回-1
-
fd文件标识符系统自带的0、1、2分别代表标准输入、标准输出和标准错误输出
分别用常量STDIN_FILENO、STDOUT_FILENO和STDERR_FILENO代替。 -
习惯上,标准输入(standard input)的文件描述符是 0,标准输出(standard output)是 1,标准错误(standard error)是 2。
-
0,1,2对应的物理设备一一般是:键盘,显示器,显示器。
-
所以通常当我们成功打开文件时,返回的fd都是从3起
#include <sys/types.h> //opne、lseek #include <sys/stat.h> //opne #include <fcntl.h> //opne int fd; fd = open("./file2",O_RDWR|O_CREAT|O_EXCL,0600);
write:写入文件
ssize_t write(int fd, const void *buf, size_t count);
fd:文件标识符,控制那个文件的标识名称
buf:存放需要写入的内容
count:写入多大的内容
#include <unistd.h>
int fd;
write(fd,Buf,strlen(Buf));
read:读取文件
ssize_t read(int fd, void *buf, size_t count);
fd:文件标识符,控制那个文件的标识名称
buf:存放读取到文件fd的内容
count:读取多大的内容
#include <unistd.h>
int fd;
read(fd,readBuf,n_write);
close:关闭文件
int close(int fd);
fd:需要关闭文件的文件标识符
#include <unistd.h>
int fd;
read(fd,readBuf,n_write);
#include <stdio.h> //C语言基础头文件
#include <stdlib.h> //malloc
#include <string.h> //strlen
#include <sys/types.h> //opne、lseek
#include <sys/stat.h> //opne
#include <fcntl.h> //opne
#include <unistd.h> //write、read、close、lseek
int main()
{
int fd;
//建立文件标识符
char *Buf = "yangyingchun hen shuai!";
//创建指针地址静态文本
fd = open("./file1",O_RDWR);
//打开file1文件,文件权限可读可写
if(fd == -1){
printf("open file1 failed\n");
//当打开文件file1失败
fd = open("./file1",O_RDWR|O_CREAT,0600);
//打开没有file1文件时,创建新的可读可写file1文件,权限可读可写
if(fd > 0){
printf("create file1 success!\n");
//打开成功
}
}
printf("open susceess : fd = %d\n",fd);
int n_write = write(fd,Buf,strlen(Buf));
//写入Buf指针的文本到file1打开的文件里
if(n_write !=-1){
printf("write %d byte to file1\n",n_write);
}
close(fd);
//关闭file1文件
fd = open("./file1",O_RDWR);
//打开file1文件
char *readBuf;
//创建读取缓存区
readBuf = (char *)malloc(sizeof(char)*n_write + 1);
//给读取缓存区开辟空间
memset(readBuf,0,sizeof(readBuf));
//初始化缓存空间
int n_read = read(fd,readBuf,n_write);
//把打开的文件内容读取到readBuf
printf("read %d ,context:%s\n",n_read,readBuf);
close(fd);
//关闭打开的file1文件
return 0;
}
光标
lseek:光标控制
off_t lseek(int fd, off_t offset, int whence);
fd:控制光标的文件
offset:相对光标现在位置的偏移值,可为正,负数,正数向后偏移,负数向后偏移
whence:让光标指向位置
SEEK_SET 让光标指向文件的头
SEEK_CUR 让光标指向当前位置
SEEK_END 让光标指向尾部
#include <sys/types.h>
#include <unistd.h>
lseek(fd, 0, SEEK_SET);
#include <stdio.h> //C语言基础头文件
#include <stdlib.h> //malloc
#include <string.h> //strlen
#include <sys/types.h> //opne、lseek
#include <sys/stat.h> //opne
#include <fcntl.h> //opne
#include <unistd.h> //write、read、close、lseek
int main()
{
int fd;
//建立文件标识符
char *Buf = "yangyingchun hen shuai!";
//创建指针地址静态文本
fd = open("./file1",O_RDWR);
//打开file1文件,文件权限可读可写
if(fd == -1){
printf("open file1 failed\n");
//当打开文件file1失败
fd = open("./file1",O_RDWR|O_CREAT,0600);
//打开没有file1文件时,创建新的可读可写file1文件,权限可读可写
if(fd > 0){
printf("create file1 success!\n");
//打开成功
}
}
printf("open susceess : fd = %d\n",fd);
int n_write = write(fd, Buf, strlen(Buf));
//写入Buf指针的文本到file1打开的文件里
if(n_write !=-1){
printf("write %d byte to file1\n",n_write);
}
char *readBuf;
//创建读取缓存区
readBuf = (char *)malloc(sizeof(char)*n_write + 1);
//给读取缓存区开辟空间
memset(readBuf,0,sizeof(readBuf));
//初始化缓存空间
//lseek(fd, 0, SEEK_SET);
//将光标移动到文件开头
lseek(fd, -n_write, SEEK_CUR);
//将光标从现在的位置向后偏移n_write个字符位
int n_read = read(fd, readBuf, n_write);
//把打开的文件内容读取到readBuf
printf("read %d ,context:%s\n",n_read,readBuf);
close(fd);
}
判断文件是否存在
open一个文件,配合打开方式O_RDWR|O_CREAT|O_EXCL,权限0600使用,
此类情况下来判断此文件是否存在,存在则返回错误,
不存在则创建文件。
#include <stdio.h> //C语言基础头文件
#include <sys/types.h> //opne、lseek
#include <sys/stat.h> //opne
#include <fcntl.h> //opne
#include <unistd.h> //write、read、close、lseek
int main()
{
int fd;
fd = open("./file2",O_RDWR|O_CREAT|O_EXCL,0600);
if(fd == -1){
printf("file1 exist!\n");
}
close(fd);
return 0;
}
写入文件内容存在,则覆盖原有内容方式写入
open一个文件,配合打开方式O_RDWR使用,
此类情况下写入文件,打开的文件有内容,则覆盖原有内容写入
#include <stdio.h> //C语言基础头文件
#include <stdlib.h> //malloc
#include <string.h> //strlen
#include <sys/types.h> //opne、lseek
#include <sys/stat.h> //opne
#include <fcntl.h> //opne
#include <unistd.h> //write、read、close、lseek
int main()
{
int fd;
char *Buf = "yangyingchun hen shuai!";
fd = open("./file1",O_RDWR);
if(fd > 0){
printf("open file1 succeed!\n");
}
int n_write = write(fd, Buf, strlen(Buf));
if(n_write !=-1){
printf("write %d byte to file1\n",n_write);
}
close(fd);
return 0;
}
写入文件存在内容,则写入内容追加到原有内容的尾部写入
open一个文件,配合打开方式O_RDWR|O_CREAT|O_APPEND,权限0600使用,
此类情况下写入文件,来判断此文件是否有内容,没有内容就直接写入,有则追加到文件尾部写入
#include <stdio.h> //C语言基础头文件
#include <stdlib.h> //malloc
#include <string.h> //strlen
#include <sys/types.h> //opne、lseek
#include <sys/stat.h> //opne
#include <fcntl.h> //opne
#include <unistd.h> //write、read、close、lseek
int main()
{
int fd;
char *Buf = "yangyingchun hen shuai!";
fd = open("./file2",O_RDWR|O_CREAT|O_APPEND,0600);
if(fd > 0){
printf("open file2 succeed!\n");
}
int n_write = write(fd, Buf, strlen(Buf));
if(n_write !=-1){
printf("write %d byte to file1\n",n_write);
}
close(fd);
return 0;
}
写入文件存在内容,则删除原有内容,在写入新内容
open一个文件,配合打开方式O_RDWR|O_TRUNC,使用,
此类情况下写入文件,则是把存在的内容删除,在写入新内容
#include <stdio.h> //C语言基础头文件
#include <stdlib.h> //malloc
#include <string.h> //strlen
#include <sys/types.h> //opne、lseek
#include <sys/stat.h> //opne
#include <fcntl.h> //opne
#include <unistd.h> //write、read、close、lseek
int main()
{
int fd;
char *Buf = "yangyingchun hen shuai!";
fd = open("./file2",O_RDWR|O_TRUNC,0600);
if(fd > 0){
printf("open file2 succeed!\n");
}
int n_write = write(fd, Buf, strlen(Buf));
if(n_write !=-1){
printf("write %d byte to file1\n",n_write);
}
close(fd);
return 0;
}
Creat函数用法
#include <sys/types.h> // 提供mode_t类型
#include <sys/stat.h> // 提供open()函数的符号
#include <fcntl.h> // 提供open()函数
#include <unistd.h> // 提供close()函数
int creat(const char * pathname, mode_t mode);
pathname:要创建的文件名(包含路径,缺省为当前路径)
mode:创建模式 //可读可写可执行
常见创建模式:
宏 数字
S_IRUSR 4 可读
S_IWUSR 2 可写
S_IXUSR 1 可执行
S_IRWXU 7 可读、可写、可执行
返回值:文件描述符,值为一个整数,发生错误时返回-1
#include <stdio.h> //C语言基础头文件
#include <stdlib.h> //malloc
#include <string.h> //strlen
#include <sys/types.h> //creat、lseek
#include <sys/stat.h> //creat
#include <fcntl.h> //creat
#include <unistd.h> //write、read、close、lseek
int main()
{
int fd;
char *Buf = "yangyingchun hen shuai!";
fd = creat("./file2",S_IRWXU);
if(fd > 0){
printf("open file2 succeed!\n");
}
int n_write = write(fd, Buf, strlen(Buf));
if(n_write !=-1){
printf("write %d byte to file1\n",n_write);
}
close(fd);
return 0;
}