目录
一、函数头文件介绍
在所有的 linux 系统中,如果需要对文件的进行操作,只要包含如下 4 个头文件即可。
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
上面四个头文件中包含了打开,关闭,创建,读文件,写文件的函数,还有标志位,以及在不同 32 位以及 64 位系统下数据长度的宏变量定义。
二、 打开文件函数open
1.open函数解析
使用 open 函数的时候会返回一个文件句柄,文件句柄是文件的唯一识别符 ID。对文件的操作必须从读取句柄开始。
open 的两个原型:
int open(const char *path, int oflags);
int open(const char *path, int oflags,mode_t mode);
第一个参数 path 表示:路径名或者文件名。路径名为绝对路径名,例如开发板中的 led驱动的设备几点/dev/leds。
第二个参数 oflags 表示:打开文件所采取的动作。
下面三个选项是必须选择其中之一的。
O_RDONLY 文件只读
O_WRONLY 文件只写
O_RDWR 文件可读可写
下面是可以任意选择的。
O_APPEND 每次写操作都写入文件的末尾O_CREAT 如果指定文件不存在,则创建这个文件
O_EXCL 如果要创建的文件已存在,则返回 -1,并且修改 errno 的值
O_TRUNC 如果文件存在,并且以只写/读写方式打开,则清空文件全部内容
O_NOCTTY 如果路径名指向终端设备,不要把这个设备用作控制终端。
O_NONBLOCK 如果路径名指向 FIFO/块文件/字符文件,则把文件的打开和后继 I/O
设置为非阻塞模式(nonblocking mode),后面会介绍什么是阻塞和非阻塞。
O_NDELAY 和 O_NONBLOCK 功能类似,调用 O_NDELAY 和使用的
O_NONBLOCK 功能是一样的。 第三个参数 mode 表示:设置创建文件的权限。S_IRUSR,S_IWUSER,S_IXUSR,S_IRGRP,S_IWGRP,S_IXGRP,S_IROTH,S_IWOTH,S_IXOTH.其中 R:读,W:写,X:执行,USR:文件所属的用户,GRP:文件所属的组,OTH:其他用户。第三个参数可以直接使用参数代替。
2.open函数例程
#include <stdio.h>
#include <sys/types.h> //man 2 open
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
int fd;
char *leds = "/dev/leds";
char *test1 = "/bin/test1";
if((fd = open(leds,O_RDWR|O_NOCTTY|O_NDELAY)) < 0)
{
printf("open %s failed!\n",leds);
}
else
{
printf("%s fd is %d\n",leds,fd);
}
if((fd = open(test1,O_RDWR|O_CREAT,0777)) < 0)
{
printf("open %s failed!\n",test1);
}
else
{
printf("%s fd is %d\n",test1,fd);
}
return 0;
}
三、创建文件函数creat
1.creat函数解析
creat 函数原型如下:
int creat(const char * pathname, mode_t mode);
creat 函数只有两个参数,参数的含义和 open 类似。
大家看到这个函数的时候知道它是创建文件的就成,在写代码的时候完全可以用 open 代替。
2.creat函数例程
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
int fd;
char *leds = "/dev/leds";
char *test1 = "/bin/test1";
char *test2 = "/bin/test2";
if((fd = open(leds,O_RDWR|O_NOCTTY|O_NDELAY)) < 0)
{
printf("open %s failed!\n",leds);
}
else
{
printf("%s fd is %d\n",leds,fd);
}
if((fd = open(test1,O_RDWR|O_CREAT,0777)) < 0)
{
printf("open %s failed!\n",test1);
}
else
{
printf("%s fd is %d\n",test1,fd);
}
fd = creat(test2,0777);
if(fd == -1)
{
printf("%s fd is %d\n",test2,fd);
}
else
{
printf("create %s is succeed\n",test2);
}
return 0;
}
四、关闭文件函数close
close 函数在头文件“#include <unistd.h>”中,close 函数的使用和参数都比较简单.
int close(int fd);
参数 fd,使用 open 函数打开文件之后返回的句柄。返回值,一般很少使用 close 的返回值。
五、写文件函数write
1.write函数解析
write 函数在头文件“#include <unistd.h>”中。
函数原型为 ssize_t write(int fd,const void *buf,size_t count)
参数 fd,使用 open 函数打开文件之后返回的句柄。
参数*buf,需要写入的数据。
参数 count,将参数*buf 中最多 count 个字节写入文件中。
返回值为 ssize 类型,出错会返回-1,其它数值表示实际写入的字节数。
2.write函数例程
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main()
{
int fd;
char *testwrite = "/bin/testwrite";
ssize_t length_w;
char buffer_write[] = "hello meng!";
fd = open(testwrite,O_RDWR|O_CREAT,0777);
if(fd < 0){
printf("open %s failed\n",testwrite);
}
length_w = write(fd,buffer_write,strlen(buffer_write));
if(length_w == -1){
perror("write");
}else{
printf("OK\n");
}
close(fd);
return 0;
}
六、读文件函数read
1.read函数解析
read 函数在头文件“#include <unistd.h>”中。
函数原型为 ssize_t read(int fd,void *buf,size_t len)参数 fd,使用 open 函数打开文件之后返回的句柄。
参数*buf,读出的数据保存的位置。
参数 len,每次最多读 len 个字节。
返回值为 ssize 类型,出错会返回-1,其它数值表示实际写入的字节数,返回值大于 0 小于 len 的数值都是正常的。
2.read函数例程
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#define MAX_SIZE 1000
int main()
{
int fd;
char *testwrite = "/bin/testwrite";
ssize_t length_w,length_r = MAX_SIZE,ret;
char buffer_write[] = "hello meng!";
char buffer_read[MAX_SIZE];
fd = open(testwrite,O_RDWR|O_CREAT,0777);
if(fd < 0){
printf("open %s failed\n",testwrite);
}
length_w = write(fd,buffer_write,strlen(buffer_write));
if(length_w == -1){
perror("write");
}else{
printf("OK\n");
}
close(fd);
fd = open(testwrite,O_RDWR|O_CREAT,0777);
if(fd < 0){
printf("open %s failed\n",testwrite);
}
if(ret = read(fd,buffer_read,length_r)){
perror("read");
}else{
printf("content is %s \n",buffer_read);
}
close(fd);
return 0;
}