系统编程之文件操作

    关于文件的操作,主要的函数是open,close,write,read,lseek,mmap,这几个函数,因为到现在再把以前的文操作再复习一边,都是比较简单的操作,都不多说。直接给出代码::

    1,open write函数:

    

  1 #include <stdio.h>
  2 #include <unistd.h>
  3 #include <stdlib.h>
  4 #include <string.h>
  5 #include <fcntl.h>
  6 
  7 
  8 int main()
  9 {
 10     int fd =open("hong.txt",O_CREAT|O_RDWR,0777);
 11     if(fd < 0)
 12     {
 13         perror("open");
 14         exit(1);
 15     }
 16 
 17     printf("open file discribler is %d 
",fd);
 18 
 19     char* buf ="hello binary_tree!
";
 20 
 21     fd=write(fd,buf,strlen(buf));
 22     if(fd < 0)
 23     {
 24         perror("write");
 25         exit(1);
 26     }
 27 
 28     close(fd);
 29 }
~                                                                                                                                                  
~                                                                                                                                                  
~               

    运行结果:

    

    2.read

    

  1 #include <stdio.h>
  2 #include <unistd.h>
  3 #include <stdlib.h>
  4 #include <string.h>
  5 #include <fcntl.h>
  6 
  7 
  8 int main()
  9 {
 10     int fd =open("hong.txt",O_CREAT|O_RDWR,0777);
 11     if(fd < 0)
 12     {
 13         perror("open");
 14         exit(1);
 15     }
 16 
 17     printf("open file discribler is %d 
",fd);
 18 
 19     char buf[30];
 20     memset(buf,0,30);
 21 
 22     int fp=read(fd,buf,1023);
 23     if(fp < 0)
 24     {
 25         perror("read");
 26         exit(1);
 27     }
 28     printf("read buf is %s
",buf);
 29 
 30     fp=write(1,buf,sizeof(buf));
 31     if(fp < 0)
 32     {
 33         perror("write");
 34         exit(1);
 35     }
 36 
 37     close(fd);
 38 }
~                                                                                                                                                  
~                                                                                                                                                  
~                                                                                                                                                  
~                           

  运行结果:

      

  3.mmap:将文件映射到内存操作。

      

      

  1 #include <stdio.h>
  2 #include <unistd.h>
  3 #include <stdlib.h>
  4 #include <string.h>
  5 #include <fcntl.h>
  6 #include <sys/mman.h>
  7 #include <sys/stat.h>
  8 
  9 
 10 int main()
 11 {
 12     int fd =open("hong.txt",O_CREAT|O_RDWR,0777);
 13     if(fd < 0)
 14     {
 15         perror("open");
 16         exit(1);
 17     }
 18 
 19     struct stat my;
 20     int fp = fstat(fd,&my);
 21     if(fp < 0)
 22     {
 23         perror("fstat
");
 24         exit(1);
 25     }
 26 
 27 
 28     char* addr= mmap(NULL,my.st_size,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
 29     if(addr == (void*)-1)
 30     {
 31         perror("mmap
");
 32         exit(1);
 33     }
 34     printf("addr is %s
",addr);
 35     strcpy(addr,"this is new string
");
 36 
 37     char buf[40];
 38     memset(buf,0,40);
 39     fp =read(fd,buf,40);
 40     if(fp < 0)
 41     {
 42         perror("open");
 43         exit(1);
 44     }
 45     printf("read buf is %s
",buf);
 46 
 47     munmap(addr,my.st_size);
 48     addr=NULL;
 49     close(fd);
 50 }
~              

     总结:

       关于文件操作,都是比较简单的操作,其中,比较难掌握的是mmap,其实作用就是将文件映射到内错单元,通过操作内存来操作文件,值得注意的是一些小问题,比如,关闭文件,释放映射的单元,memset的初始化之类的问题,还有,如果是在读写文件的时候,特别是读的时候,发现读出的数据异常,极有可能是lseek读写指针的问题,所以要主要清零的问题。其他的问题,如果是忘了函数参数的具体。可以man手册帮助

      

原文地址:https://www.cnblogs.com/hongzhunzhun/p/4547497.html