zoukankan      html  css  js  c++  java
  • Linux环境下实现对文件读写操作

    ---- 今天分享一下在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、打开文件:

    1#include <stdio.h>
     2#include <sys/types.h>
     3#include <sys/stat.h>
     4#include <fcntl.h>
     5#include <unistd.h>
     6int main(int argc, char *argv[])
     7{
     8  //第一步:打开文件
     9  int fd=-1;    //fd是文件描述符(在linux中的文件描述符fd 
    10  的合法范围是0或者是一个正数,不可能是负数)
    11  fd=open("a.txt",O_RDWR);//O_RDWR表示文件可读可写,这个可以 
    12  用man 手册查看open函数的使用方法里面有它的说明​
    13  if(-1 ==fd)或者(fd<014  {
    15    printf("文件打开错误
    ");
    16  }
    17  else
    18  {
    19    printf("文件打开成功
    ");
    20  }
    21 //读写文件
    22 //关闭文件
    23  close(fd);//关闭刚才打开的文件
    24  return 0;                                                   
    25}

    2、读文件操作:

     1#include <stdio.h>
     2#include <sys/types.h>
     3#include <sys/stat.h>
     4#include <fcntl.h>
     5#include <unistd.h>
     6int main(int argc,char *argv[])
     7{
     8   int fd =-1;
     9   int ret=-1;
    10   char buf[100]={0};
    11   fd=open("a.txt",O_RDWR);
    12   if(-1==fd)
    13   {
    14      printf("the open the file is failure 
    ");
    15   }
    16   else
    17   {
    18      printf("the open the file is successful,fd=%d.
    ",fd);
    19   }
    20  ret=read(fd,buf,20);//20表示读取的字节
    21
    22  if(ret<-1)
    23  {
    24    printf("read失败
    ");
    25  }
    26  else
    27  {
    28     printf("成功读取了%d字节
    ",ret);
    29     printf("文件内容是:[%s]
    ",buf);
    30  }
    31  close(fd);
    32  return 0;                                               
    33}

    代码编译效果:
    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、写文件:

     1   #include <stdio.h>
     2   #include <sys/types.h>
     3   #include <sys/stat.h>
     4   #include <fcntl.h>
     5   #include <unistd.h>
     6   #include <string.h>
     7int main(int argc,char *argv[])
     8{ 
     9int fd =-1;
    10char buf[100]={0};
    11char w_buf[20]="i like the mcu";
    12int ret=-1;
    13fd=open("a.txt",O_RDWR);
    14if(-1==fd)
    15{
    16  printf("the open the file is failure 
    ");
    17}
    18else
    19{
    20  printf("the open the file is successful,fd=%d.
    ",fd);
    21}
    22  ret=write(fd,w_buf,strlen(w_buf));
    23 if(ret<0)
    24 {
    25   printf("the write is failure
    ");
    26 }
    27else
    28{
    29   printf("write successful 写入了%d个字符
    ",ret);
    30}
    31  ret=read(fd,buf,20);//20表示读取的字节
    32if(ret<-1)
    33{
    34  printf("read失败
    ");
    35}
    36else
    37{
    38   printf("成功读取了%d字节
    ",ret);
    39   printf("文件内容是:[%s]
    ",buf);
    40}
    41   close(fd);
    42   return 0;                                               
    43}

    编译结果:
    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、关闭文件

  • 相关阅读:
    #include "stdafx.h" 错误?
    扩频技术
    求数组中只出现一次的数字(算法)
    1.3一摞烙饼的排序
    嵌套类
    企业级邮件服务软件推荐
    关于Linq To Sql中Detach方法和一个公共基类
    asp.net(c#) 将dbf转换为xls或wps,并将数据的列名改成中文;并判断本机是否安装office2003,2007和wps2007,2010
    一句代码解决IE8兼容问题(兼容性视图)
    asp.net(C#)套用模板操作Excel
  • 原文地址:https://www.cnblogs.com/1121518wo/p/11616664.html
Copyright © 2011-2022 走看看