open函数

文件描述符是一个非负整数,文件描述符0:标准输入,(键盘输入),文件描述符1:标准输出,文件描述符2:标准错误(可以存放垃圾文件)

open函数打开文件,打开失败返回的fd为-1

文件存放在块设备中的文件系统文件中,叫做静态文件;

当用open打开一个文件时,linux内核做的操作包括:内核在进程中建立一个打开文件的数据结构(结构体),记录下我们打开的这个文件,内核在内存中申请一段内存,将静态文件的内容从块设备中读取到内核中特定地址管理存放(动态文件);

文件读写操作针对动态文件,当close关闭动态文件时,close内部内核将动态文件中的内容去更新块设备中的静态文件;

块设备读写按照你块进行,不灵活,内存按字节单位操作。

可读 r 4

可写 w 2

执行 x 1

0600 rw 4+2

open函数用法

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>int main()
{int fd;        fd = open("./file1",O_RDWR);        //注意open返回一个文件描述符,很重要if(fd == -1){        //文件描述符等于-1,文件打开失败printf("open file1 failed\n");fd = open("file1",O_RDWR|O_CREAT,0600);        //创建一个文件,0600是权限if(fd > 0){printf("creat file1 success!\n");}}return 0;
}  

O_CREAT和O_EXCL一起使用可以判断是否存在一个文件

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>int main()
{int fd;fd = open("file1",O_RDWR|O_CREAT|O_EXCL,0600);if(fd == -1){printf("had file!\n");}return 0;
}

O_APPEND 从文件尾部开始写入/O_TRUNC全部覆盖掉,慎用

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main()
{int fd;char *buf = "ChenLiChen hen shuai!";fd = open("./file1",O_RDWR|O_APPEND);printf("open success : fd = %d\n",fd);int n_write =  write(fd,buf,strlen(buf));if(n_write != -1){printf("write %d byte to file1\n",n_write);}close(fd);return 0;
}

write函数

// 函数原型 ssize_t write(int fd, const void *buf, size_t count);

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>        //write所需要的头文件和函数原型  可以使用 man write 查找
#include <string.h>int main()
{int fd;char *buf = "ChenLiChen hen shuai!";        //linux环境下指针是8个字节fd = open("./file1",O_RDWR);if(fd == -1){printf("open file1 failed\n");fd = open("file1",O_RDWR|O_CREAT,0600);if(fd > 0){printf("creat file1 success!\n");}}printf("open success : fd = %d\n",fd);// 函数原型 ssize_t write(int fd, const void *buf, size_t count);write(fd,buf,strlen(buf));       //将buf指向的字符串写入file中//写入此功能后write也有一个非负整数返回值close(fd);                             //关闭文件return 0;
}

read、lseek函数

//read函数原型:ssize_t read(int fd, void *buf, size_t count);

//lseek函数原型:off_t lseek(int fd, off_t offset, int whence);

SEEK_SET 将光标定位到文件头

SEEK_CUR 光标定位到当前位置

SEEK_END 将光标定位到文件尾部

#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main()
{int fd;char *buf = "ChenLiChen hen shuai!";fd = open("./file1",O_RDWR);if(fd == -1){printf("open file1 failed\n");fd = open("file1",O_RDWR|O_CREAT,0600);if(fd > 0){printf("creat file1 success!\n");}}printf("open success : fd = %d\n",fd);//ssize_t write(int fd, const void *buf, size_t count);int n_write =  write(fd,buf,strlen(buf));if(n_write != -1){printf("write %d byte to file1\n",n_write);}char *readBuf;        //缓冲区readBuf = (char *)malloc(sizeof(char)*n_write+1);//ssize_t read(int fd, void *buf, size_t count);lseek(fd,0,SEEK_SET);        //光标定位到文件头,注意读取文件信息是从光标位置开始读取int n_read = read(fd,readBuf,n_write);printf("read  %d ,context:%s\n",n_read,readBuf);close(fd);return 0;
}

LINUX实现cp指令

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>int main(int argc,char **argv)        //argc:参数cp Src.c Des.c=3,三个参数  argc//**argv: argv[0] argv[1] argv[2]  // 二级指针、数组指针
{int fdSrc;    //定义两个文件描述符int fdDes;char *readBuf=NULL;    //缓冲区if(argc != 3){printf("pararm error\n");    //打开文件失败退出exit(-1);}fdSrc = open(argv[1],O_RDWR);int size = lseek(fdSrc,0,SEEK_END);    //计算文件大小lseek(fdSrc,0,SEEK_SET);    //定位光标readBuf = (char *)malloc(sizeof(char)*size+8);int n_read = read(fdSrc,readBuf,size);fdDes = open(argv[2],O_RDWR|O_CREAT|O_TRUNC,0600);int n_write = write(fdDes,readBuf,strlen(readBuf));close(fdSrc);    //关闭文件将动态文件更新到静态文件close(fdDes);return 0;
}

LINUX实现 配置文件的修改

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
// ./a.out TEST.config运行
int main(int argc,char **argv)
{int fdSrc;char *readBuf=NULL;if(argc != 2){printf("pararm error\n");exit(-1);}fdSrc = open(argv[1],O_RDWR);int size = lseek(fdSrc,0,SEEK_END);lseek(fdSrc,0,SEEK_SET);readBuf = (char *)malloc(sizeof(char)*size+8);int n_read = read(fdSrc,readBuf,size);char *p = strstr(readBuf,"LENG=");    //查找字符串,返回首字母的地址if(p==NULL){printf("not found!\n");exit(-1);}p = p+strlen("LENG=");//将指针移到要修改的字符位置*p = '5';lseek(fdSrc,0,SEEK_SET);    //定位光标int n_write = write(fdSrc,readBuf,strlen(readBuf));close(fdSrc);return 0;
}

LINUX实现 写结构体数组到文件中

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
//写一个结构体到文件中
struct Test
{int a;char c;
};int main()
{int fd;struct Test data ={100,'a'};struct Test data2;fd = open("./file1",O_RDWR);int n_write = write(fd,&data,sizeof(struct Test));lseek(fd,0,SEEK_SET);int n_read = read(fd,&data2,sizeof(struct Test));printf("read = %d,%c",data2.a,data2.c);close(fd);return 0;
}

写入结构体数组到文件中

#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>struct Test
{int a;char c;
};int main()
{int fd;struct Test data[2] ={{100,'a'},{101,'b'}};struct Test data2[2];fd = open("./file1",O_RDWR);int n_write = write(fd,&data,sizeof(struct Test)*2);lseek(fd,0,SEEK_SET);int n_read = read(fd,&data2,sizeof(struct Test)*2);printf("read = %d,%c\n",data2[0].a,data2[0].c);printf("read = %d,%c\n",data2[1].a,data2[1].c);close(fd);return 0;
}

LINUX系统编程__文件编程__LINUX库open等函数的应用相关推荐

  1. LINUX系统编程__文件编程__open与fopen的区别

    1. 来源 从来源的角度看,两者能很好的区分开,这也是两者最显而易见的区别: open是UNIX系统调用函数(包括LINUX等),返回的是文件描述符(f'd),它是文件在文件描述符表里的索引: fop ...

  2. 理解Unix/Linux系统中的文件描述符

    简介 文件描述符是针对Unix/Linux的每个进程而言的,每个进程都维护了一个文件指针表,指针指向操作系统的文件.这里的文件是指的Unix/Linux系统所说的文件,Unix/Linux下一切皆文件 ...

  3. Linux系统根目录下文件是啥

    Linux系统根目录下文件是啥 在linux系统,根目录"/"中的一些重要的文件夹: /bin 存储一些二进制可执行命令文件,/usr/bin 也存放了一些基于用户的命令文件. / ...

  4. linux系统中的文件传输

    Linux系统中的文件传输 1 实验环境 2 scp命令 3 rsync命令 3.1 rsync和scp命令对比 3.2 rsync命令用法 4 文件的归档压缩 4.1 文件归档 4.2 文件压缩 4 ...

  5. Linux系统上的文件类型

    Linux系统上的文件类型 -: 常规文件 d: directory,目录文件 b: block device,块设备文件,支持以"block"为单位进行随机访问 c: chara ...

  6. linux物理内存地址与iomem,一种Linux系统物理内存镜像文件分析方法_4

    模块信息,如图7所示,给出了本发明的实施例中 模块结构关系图,modules变量指向某一个已加载模块结构体module地址,所有已加载模 块其module形成一个双向链表,如图7所示,据此可以获取到所 ...

  7. linux mount命令衔接,Linux mount命令详解:挂载Linux系统外的文件

    Linux mount命令详解:挂载Linux系统外的文件 <Linux挂载>一节讲到,所有的硬件设备必须挂载之后才能使用,只不过,有些硬件设备(比如硬盘分区)在每次系统启动时会自动挂载, ...

  8. Android 系统(68)---使用Xshell在Windows系统和Linux系统之间进行文件传输

    使用Xshell在Windows系统和Linux系统之间进行文件传输 Windows系统在安装虚拟机centos系统之后,如何进行两者之间的文件传输和互操作,或者如何在Windows端使用Xshell ...

  9. linux系统 mysql日志文件太大。造成数据库无法正常启动怎么解决

    linux系统 mysql日志文件太大.造成数据库无法正常启动怎么解决 删除mysql日志: 执行:/usr/local/mysql/bin/mysql -u root -p 输入密码登录后再执行:r ...

最新文章

  1. VS.net 2005 试用(1)
  2. 语音任务关键--噪声未必能听得到
  3. apache与tomcat的联系
  4. 各种页面刷新代码大全,asp/javascript刷新页面代码
  5. VTK修炼之道28:图像统计_灰度直方图计算
  6. CCF201809-2 买菜
  7. 【codevs1993】草地排水,网络流入门(dinic+ispa)
  8. linux装回windows系统,将全盘安装的linux换回windows
  9. 手机APP ~ MUI——创建页面方法
  10. 如何制作通讯录vcf_批量信息从表格导入手机“通讯录”
  11. 【每天学点管理学】目标管理工具——SMART法则
  12. MD通过vscode生成PDF(带目录标签)
  13. codewars 7×7 Skyscrapers 问题解决
  14. HTB_Secret
  15. 如何给外行解释云计算
  16. python下载酷狗音乐上的歌曲
  17. 快速下载官方网站软件
  18. Unity 六边形地图系列(二十五) :水循环
  19. xm在线转换成mp3_全民K歌月花费25万,95后表示在线K歌可以换一种玩法
  20. 开荒手册3——构思一篇小论文

热门文章

  1. 什么是内测分发?怎么样进行内测分发?
  2. Silverlight 版 C1OutlookBar 初体验
  3. CSS样式表的引入方式
  4. 职场人怎样提高说话技巧
  5. 01.Mac默认zsh
  6. Unity3D ML-Agent-0.8.1 学习三(多代理学习)
  7. 副驾驶的意义_副驾驶在飞行中的作用与地位
  8. Airship再下一城,因为戴尔和AtT的合作
  9. 卷毛-网络编程基础(二)什么是socket
  10. 8月 (August)总结规划