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 synchronous

    if((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.c

all:$(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*

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

青草地溪水旁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值