Linux系统编程篇—文件编程(一)打开及创建、写入、读取
文件编程概述打开与创建(open)使用open函数需要包含三个头文件:#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>三个open的API(函数):int open(const char *pathname,int flags);//pathname:指向文件路径的指针;flags:文件权限in
文件编程概述
创建(creat)
使用open函数需要包含三个头文件:
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
一个creat的API:
int creat(const char *pathname,mode_t mode);
pathname:指向文件路径的指针
int creat("/home/abl/demo",S_IRWXU);//绝对路径:/home/abl/demo
int creat("./demo",S_IRWXU);//当前路径:./demo
mode:创建模式(可读可写可执行)系统自带四个宏
可执行(1):S_IXUSR
可写(2):S_IWUSRL
可读(4):S_IRUSR
可读、可写、可执行(7):S_IRWXU
返回值
返回值为文件描述符
成功
返回一个非负整数
失败
返回-1
int fd_file1;
int creat("/home/abl/demo",S_IRWXU);//fd_file1为creat返回的文件描述符
后续的write、read等文件操作都要基于文件描述符fd。
打开(open)
使用open函数需要包含三个头文件:
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
两个open的API:
int open(const char *pathname,int flags);//pathname:指向文件路径的指针;flags:文件权限
int open(const char *pathname,int flags,mode_t mode);
int creat(const char *pathname,mode_t mode);
pathname:指向文件路径的指针
flags:文件权限
第一类:(只能3选1)
1、O_RDONLY 只读打开
2、O_WRONLY 只写打开
3、O_RDWR 可读可写打开
第二类:(可以多选)
1、O_CREAT若文件不存在则创建它,使用此选项时,需要同时说明第三个参数mode,用其说明该新文件的存取许可权限。
fd_file1=open("./file1",O_RDWR|O_CREAT,0600);//fd_file1为open返回的文件描述符,此时若不存在file1,则新建一个file1
2、O_EXCL如果同时指定了O_CREAT,而文件已经存在,则出错(返回-1)。
fd_file1=open("./file1",O_RDWR|O_CREAT|O_EXCL,0600);//fd_file1为open返回的文件描述符,此时若已经存在file1,open返回-1
3、O_APPEND每次写时都加到文件的尾端。
fd_file1=open("./file1",O_RDWR|O_APPEND);//此时可将原文件里的信息先删除后写入
4、O_TRUNC属性去打开文件时,如果这个文件中本来是有内容的,而且为只读或只写成功打开,则将其长度截短为0。
fd_file1=open("./file1",O_RDWR|O_TRUNC);//此时可将原文件里的信息先删除后写入
两类使用“|”进行连接
mode:创建模式(可读可写可执行)
可执行:0100
可写:0200
可读:0400
可读、写:0600
可读、可写、可执行:0700
一定是在flags中使用了O_CREAT标志,mode记录待创建的文件的访问权限
关于open函数的描述
DESCRIPTION
Given a pathname for a file,open()returns a file descriptor , a small,nonnegative integer for use in subsequent system calls(read(2), write(2),lseek(2),fcntl(2), etc.).The file descriptor returned by a successful call will be the lowest.
//传入一个pathname(文件路径),open返回一个文件描述符,一个小的非负整数,后续系统使用calls(read(2), write(2),lseek(2),fcntl(2),等都需要根据open返回的文件描述符来操作。
文件描述符:类似一个索引,用于区分使用open打开的各个文件,在文件打开后进行后续操作都必须通过文件描述符来判断是哪一个文件。当一个文件被打开后,系统会产生一个结构体来存储这个文件的一些信息,而文件描述符会指向这些结构体。
open的返回值(文件描述符)很重要
与craet的返回值相同
int fd_file1;
fd_file1=open("./file1",O_RDWR);//fd_file1为open返回的文件描述符
后续的write、read等文件操作都要基于文件描述符fd。
写入(write)
需要包含的头文件:
#include <unistd.h>
函数原型:
ssize_t write(int fd,const void *buf,size_f count);
函数描述
DESCRIPTION
write() writes up to count bytes from the buffer pointed buf to the file referred to by the file descriptor fd.
//从buffer中写入count个字节的数据到文件描述符为fd的文件中。
传入参数:
int fd:fd为文件的文件描述符
const void *buf:buf为须要写入的字节
size_f count:写入了多少长度的字节
返回值:
成功:返回写入字节的个数(一个整型数)
失败:返回-1;
示例:
int main()
{
int fd_file1;
char* buf="bobob";
fd_file1=open("./file1",O_RDWR);
if(fd_file1 = -1){//判断文件是否可以打开
printf("open failed\n");
fd_file1 = open("./file1",O_RDWR|O_CREAT,0600);//打开文件失败,新建一个名叫file1的文件,权限为6(代表可读、可写)
if(fd_file1 > 0){
printf("creat success!\n");
}
}
printf("open file1 success");
write(fd_file1,buf,strlen(buf));//write函数(fd变量为fd_file1,buf变量为buf,count变量为buf字符串的长度)
printf("fd_file1=%d\n",fd_file1);
close(fd_file1);
return 0;
}
执行.out文件:
vi file1
读取(read)
需要包含的头文件:
#include <unistd.h>
函数原型:
ssize_t read(int fd,const void *buf,size_f count);
函数描述
DESCRIPTION
read() attempts to read up to count bytes from file descriptor fd into the buffer starting at buf.
//read()从文件描述符fd读取count个字节的数据到从buf开始的缓冲区中。
传入参数:
int fd:fd为文件的文件描述符
const void *buf:buf为须要读出的字节存放的缓冲区
size_f count:读出了多少长度的字节
返回值:
成功:返回读取字节的个数(一个整型数)
失败:返回-1;
示例
int main()
{
int fd_file1;
fd_file1 = open("./file1",O_RDWR);//打开文件
char *readbuf=(char*)malloc(sizeof(char)*writedata_num);
int readdata_num=read(fd_file1,readbuf,writedata_num);
close(fd_file1);
printf("read %d,contex %s\n",readdata_num,readbuf);
return 0;
}
更多推荐
所有评论(0)