当前位置: 首页 > news >正文

linux的系统调用open, read函数(文件编程)使用demo

1.引言

为了学习linux系统下的app开发,记载了学习文件编程的笔记

2.open函数

功能

        打开一个文件

头文件

        #include<sys/stat.h> #include<fcntl.h>

函数形式

         int open(const char* pathname, int flags, mode_t mode);

返回值

        如果调用成功,则返回文件描述符号,标识文件资源,后续会使用。 如果调用出错,则会返回-1

参数

        pathname:打开的文件名(含路径)。

        flags: 文件访问模式的bit mask。

        mode: 文件权限模式

3. 使用案例

copy文件的案例

文件如下,名字:copy_file.c

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h> 
#include <errno.h>#define BUFFER_SIZE 1024 int main(int argc,char *argv[]) 
{ int from_fd,to_fd; int bytes_read,bytes_write; char buffer[BUFFER_SIZE]; char *ptr; //参数防卫,命令选项必须输入3个参数if(argc!=3) { fprintf(stderr,"Usage:%s fromfile tofile/n/a",argv[0]); exit(1); } //以只读的方式打开第一个参数(文件路径)if((from_fd=open(argv[1],O_RDONLY))==-1) { //打开失败fprintf(stderr,"Open %s Error:%s/n",argv[1],strerror(errno)); exit(1); } // Constant Octal	value	Permission	bit
// S_ISUID	04000	Set-user-ID
// S_ISGID	02000	Set-group-ID
// S_ISVTX	01000	Sticky
// S_IRUSR	0400	User-read
// S_IWUSR	0200	User-write
// S_IXUSR	0100	User-execute
// S_IRGRP	040	    Group-read
// S_IWGRP	020	    Group-write
// S_IXGRP	010	    Group-execute
// S_IROTH	04	    Other-read
// S_IWOTH	02	    Other-write
// S_IXOTH	01	    Other-execute// Flag         describtion
// O_RDONLY     Open for reading only v3
// O_WRONLY     Open for writing only v3
// O_RDWR       Open for reading and writing v3
// O_CLOEXEC    Set the close-on-exec flag (since Linux 2.6.23) v4
// O_CREAT      Create file if it doesn’t already exist v3
// O_DIRECT     File I/O bypasses buffer cache
// O_DIRECTORY  Fail if pathname is not a directory v4
// O_EXCL       With O_CREAT: create file exclusively v3
// O_LARGEFILE  Used on 32-bit systems to open large files
// O_NOATIME    Don’t update file last access time on read() (since Linux 2.6.8)
// O_NOCTTY     Don’t let pathname become the controlling terminal v3
// O_NOFOLLOW   Don’t dereference symbolic links v4
// O_TRUNC      Truncate existing file to zero length v3
// O_APPEND     Writes are always appended to end of file v3
// O_ASYNC      Generate a signal when I/O is possible
// O_DSYNC      Provide synchronized I/O data integrity (since Linux 2.6.33) v3
// O_NONBLOCK   Open in nonblocking mode v3
// O_SYNC       Make file writes synchronousif((to_fd=open(argv[2],O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR))==-1) { fprintf(stderr,"Open %s Error:%s/n",argv[2],strerror(errno)); exit(1); } while(bytes_read=read(from_fd, buffer, BUFFER_SIZE)) { if((bytes_read==-1)&&(errno!=EINTR)) {break;}    else if(bytes_read > 0) { ptr = buffer; while(bytes_write=write(to_fd,ptr,bytes_read)) { if((bytes_write==-1)&&(errno!=EINTR))break; else if(bytes_write==bytes_read)break; else if(bytes_write>0) { ptr+=bytes_write; bytes_read-=bytes_write; } } if(bytes_write==-1)break; } } close(from_fd); close(to_fd); exit(0); 
} 

编译用Makefile

TARGET := app
#src file
SRC := copy_file.call:$(TARGET)@echo "make successfull"$(TARGET): $(SRC)@echo $(SRC)gcc  $^ -I. -o $@clean:rm $(TARGET).PHONY:all,clean

ubuntu系统下使用gcc编译通过,运行实例如下

终端命令:./app copy_file.c a.c
结果展示命令: ll
total 41
drwxrwxrwx 1 root root  4096 May 25 23:05 ./
drwxrwxrwx 1 root root     0 Mar  8 21:31 ../
-rwxrwxrwx 1 root root  3165 May 25 23:05 a.c*    --->(复制成功的新文件)
-rwxrwxrwx 1 root root 17056 May 25 23:04 app*
-rwxrwxrwx 1 root root  3165 May 25 23:04 copy_file.c*

http://www.lryc.cn/news/353222.html

相关文章:

  • C语言基础——循环(2)+关机程序
  • cnVcXsrv 21.1.13.1—VcXsrv 21.1.13中文版本简单说明~~
  • 心链2---前端开发(整合路由,搜索页面,用户信息页开发)
  • wordpress主题模板兔Modown 9.1开心版附送erphpdown v17.1插件
  • openai api的初次尝试
  • Distributed Transactions Mit 6.824
  • Redis可视化工具:Another Redis Desktop Manager下载安装使用
  • Parquet文件格式详解(含行、列式存储区别)
  • 一文了解https为什么是安全的
  • [‘column‘]和[:,‘column‘]的区别
  • icloud如何高效利用
  • k8s二进制安装与部署
  • 驱动编译报error: negative width in bit-field ‘<anonymous>’错误
  • Go语言的命名规范是怎样的?
  • Vue3骨架屏(Skeleton)
  • 【文末附gpt升级方案】亚马逊与Hugging Face合作:定制芯片低成本运行AI模型的创新探索
  • 二叉树的链式实现
  • STM32中断编程入门
  • 《我的阿勒泰》读后感
  • Android.mk简单介绍、规则与基本格式
  • 【MySQL精通之路】InnoDB(3)-MVCC多版本管理
  • uniapp 对接 微信App/支付宝App 支付
  • cmake配置opencv与boost库
  • 【Kotlin 一】Kotlin入门知识简介、变量声明、数字类型
  • Java 微信小程序登录(openId方式)
  • 为何程序员35岁就开始被嫌弃了?程序员该如何避免中年危机?
  • 【2024软考】史上最全!软考刷题+解析大合集(9万字全手工打,货真价实)
  • 【Spring Security + OAuth2】授权
  • 失落的方舟台服预下载教程 一键下载+账号注册教程
  • 【启明智显技术分享】SOM2D02-2GW核心板适配ALSA(适用Sigmastar ssd201/202D)