---- 今天分享一下在linux系统在实现对文件读写一些基本的操作,在这之前我们要掌握一些基本的技能在Linux环境下。比如查看命令和一个函数的具体用法,就是相当于查手册,在Linux下有一个man手册非常有用:
man查询手册
man 1 +命令 这里的1表示为查询的是Linux命令
man 2 xxx 这里的2表示为查询的是linux api
man 3 xxx 这里的3表示为查询的是c库函数
在了解了这个后我们就可以开始来实现标题说的操作了。
一、在linux环境下常用文件接口函数:open、close、write、read、lseek。
二、文件操作的基本步骤分为:
a、在linux系统中要操作一个文件,一般是先open打开一个文件,得到一个文件扫描描述符,然后对文件进行读写操作(或其他操作),最后关闭文件即可。
b、对文件进行操作时,一定要先打开文件,然后再进行对文件操作(打开文件不成功的话,就操作不了),最后操作文件完毕后,一定要关闭文件,否则可能会造成文件损坏
c、文件平时是存放在块设备中的文件系统中的,我们把这个文件叫做静态文件,当我们去打开一个文件时,linux内核做的操作包括:内核在进程中建立了一个打开文件的数据结构,
记录下我们打开的这个文件,内核在内存中申请一段内存,并且将静态文件的内容从块设备中读取到内存中特定地址管理存放(叫动态文件)
d、打开文件后,以后对这个文件的读写操作,都是针对内存中这一份动态文件的,而不是针对静态文件的。
当我们对动态文件进行读写后,此时内存中的动态文件和块设备中的静态文件就不同步了,
当我们close 关闭动态文件时,close内部内核将内存中的动态文件的内容去更新(同步)块设备中的静态文件。
三、为什么是这样操作?
以块设备本身有读写限制(回忆Nandflash、SD、等块设备的读写特征),本身对块设备进行操作非常不灵活。而内存可以按字节为单位来操作。而且进行随机操作。
四、文件描述符是什么?
1、文件描述符:它其实实质是一个数字,这个数字在一个进程中表示一个特定的含义,当我们open打开一个文件时,操作系统在内存中构建了一些数据结构来表示这个动态文件,然后返回给应用程序一个数字作为文件描述符,这个数字就和我们内存中维护这个动态文件的这些数据结构挂钩绑定上了。以后我们应用程序如果要操作这一个动态文件,只需要用这个文件描述符进行区分。简单来说,它是来区分多个文件的(在打开多个文件的时候)。
2、文件描述的作用域就是当前的进程,出了这个当前进程,这个文件描述符就没有意义了。
五、代码实现:
1、打开文件:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>int main(int argc, char *argv[])
{
//第一步:打开文件
int fd=-; //fd是文件描述符(在linux中的文件描述符fd
的合法范围是0或者是一个正数,不可能是负数)
fd=open("a.txt",O_RDWR);//O_RDWR表示文件可读可写,这个可以
用man 手册查看open函数的使用方法里面有它的说明
if(- ==fd)或者(fd<)
{
printf("文件打开错误\n");
}
else
{
printf("文件打开成功\n");
}
//读写文件
//关闭文件
close(fd);//关闭刚才打开的文件
return ;
}
2、读文件操作:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>int main(int argc,char *argv[])
{
int fd =-;
int ret=-;
char buf[]={};
fd=open("a.txt",O_RDWR);
if(-==fd)
{
printf("the open the file is failure \n");
}
else
{
printf("the open the file is successful,fd=%d.\n",fd);
}
ret=read(fd,buf,);//20表示读取的字节 if(ret<-)
{
printf("read失败\n");
}
else
{
printf("成功读取了%d字节\n",ret);
printf("文件内容是:[%s]\n",buf);
}
close(fd);
return ;
}
代码编译效果:
root@ubuntu-virtual-machine:/mnt/hgfs/day# gcc file1.c
root@ubuntu-virtual-machine:/mnt/hgfs/day# ./a.out
the open the file is successful,fd=3.
成功读取了13字节
文件内容是:[hello world!
]
3、写文件:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>int main(int argc,char *argv[])
{
9int fd =-;
10char buf[]={};
11char w_buf[]="i like the mcu";
12int ret=-;
13fd=open("a.txt",O_RDWR);
14if(-==fd)
{
printf("the open the file is failure \n");
}
18else
{
printf("the open the file is successful,fd=%d.\n",fd);
}
ret=write(fd,w_buf,strlen(w_buf));
if(ret<)
{
printf("the write is failure\n");
}
27else
{
printf("write successful 写入了%d个字符\n",ret);
}
ret=read(fd,buf,);//20表示读取的字节
32if(ret<-)
{
printf("read失败\n");
}
36else
{
printf("成功读取了%d字节\n",ret);
printf("文件内容是:[%s]\n",buf);
}
close(fd);
return ;
}
编译结果:
root@ubuntu-virtual-machine:/mnt/hgfs/day# ./a.out
the open the file is successful,fd=3.
write successful 写入了14个字符
成功读取了0字节
文件内容是:[]
root@ubuntu-virtual-machine:/mnt/hgfs/day# cat a.txt
i like the mcuroot@ubuntu-virtual-machine:/mnt/hgfs/day# vim file1.c
这里还有一个缺陷,就是写入的文件,在读取的时候没有读取出来,这个知识点在后面会继续学到,不要急。
六、总结:
对文件的操作,一个要知道它的操作步骤:
1、打开文件
2、读写文件
3、关闭文件