Linux--关于open、file、write、read、close的相关命令——操作文件的底层调用

  目录

1.基础知识:

(1)C语言操作文件的几个库函数:

(2)操作文件有关的系统调用:

(3)系统调用方法:

2.系统调用的介绍:

(1)open:

(2)write:

​编辑 (3)read:

​编辑 

(4)close:

(5)文件描述符:

(1)写入

(2)读取

(3) 利用读和写对文件进行复制

  (4)实现类似命令: 


1.基础知识:

(1)C语言操作文件的几个库函数:

fopen,fread,fwrite,fclose;

(2)操作文件有关的系统调用:

open,read,write,close;

(3)系统调用方法:

实现在内核中;(陷入内核,切换到内核)

2.系统调用的介绍:

(1)open:

e64e3981315d421482d6adc447db74df.png

 open重载:两个参数用于打开一个已经存在的文件;三个参数的用于新建一个文件,并

设置访问权限;

pathname:文件和路径和名称;

flags:文件的打开方式;

mode:文件的权限,如"0600";

open的返回值为int,称为文件描述符;

flags的打开标志,如:

O_WRONLY:只写打开;

O_RDONLY:只读打开;

O_RDWR:读写方式打开;

O_CREAT:文件不存在则创建;

O_APPEND:文件末尾追加;

O_TRUNC:清空文件,重新写入;

(2)write:

72bf426103cb401aac47ed0b2ba55e2d.png

 fd:对应打开的文件描述符

buf:写入的文件内容;

count:要写入多少个字节;

返回值:ssize_t:实际写入了多少个字节;

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<assert.h>
#include<fcntl.h>

int main()
{
    

   int fd=open("file.txt",O_WRONLY|O_CREAT,0600);//我们首先创建一个文件,全新创建需要给出权限
	//fd是一个文件描述符,用于来唯一标识我们的文件
   assert(fd!=-1);
   printf("fd=%d\n",fd);

   write(fd,"hello",5);
   
   close(fd);
   exit(0);
}

2bd20ce82f6e49009d80ff4b898c3dbb.png (3)read:

f4cacf9ffed6454d83da16dd12a98d31.png

 fd:对应打开的文件描述符;

buf:把文件内容读取到一块空间buf中;

count:期望要读取的字节数;返回值:ssize_t:实际读取了多少个字节;

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<assert.h>
#include<fcntl.h>

int main()
{

    int fd=open("file.txt",O_RDONLY);
        assert(fd!=-1);
   
    char buff[128]={0};
    int n=read(fd,buff,127);
    printf("n=%d,buff=%s\n",n,buff);
   
    close(fd);
    exit(0);
}

4cc4e14fd22c4175a5f6481fdb86e90a.png

(4)close:

关闭文件描述符;

(5)文件描述符:

文件打开以后,内核给文件的一个编号;(>0的整数)

0,1,2是默认打开的;

0:标准输入;

1:标准输出;

2:标准错误输出;

#include<stdio.h>
#include<stlib.h>
#include<unistd.h>

int main()
{
    write(1,"hello",5);
    exit(0);
}

02cfff7bc5324a548cc5e803bf7cfc4e.png

3.使用:

(1)写入

打开一个文件并往里面写入hello:

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<assert.h>
#include<fcntl.h>

int main()
{

    int fd=open("file.txt",O_RDONLY);
        assert(fd!=-1);
    printf("fd=%d\n",fd);

    write(fd,"hello",5);
   
    close(fd);
    exit(0);
}

(2)读取

打开文件,读取文件内容:

a20e08594c2a4bd8a8af8704316fa9eb.png

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<assert.h>
#include<fcntl.h>

int main()
{
    

   int fd=open("file.txt",O_WRONLY|O_CREAT,0600);
   assert(fd!=-1);
    char buff[128]={0};
    int n=read(fd,buff,127);
    printf("n=%d,buff=%s\n",n,buff);
   
   close(fd);
   exit(0);
}

04520c6a85114a61a077ab23ba6c119d.png

(3) 利用读和写对文件进行复制

我们可以通过读和写来复制一个文件,怎么做呢?我们先打开一个二进制文件,在创建一个新的文件,并且去读二进制文件将读到的数据写入新文件中,重复这个过程知道读不到数据的时候就复制完成(当read()返回值为0,即读不到数据)

#include<stdio.h>
#include<fcntl.h>
#include<stdlib.h>
#include<unistd.h>

int main()
{

    int fdr = open("file.txt",O_RDONLY);
    //只读方式打开
    int fdw = open("newfile,txt",O_WRONLY|O_CREAT,0600);
    //创建新的文件,O_CREAT 给权限
    if(fdr == -1 ||fdw == -1)//表示原文件打开或新文件创建出现错误
    {
        exit(0);
    }
    char buff[256] = {0};
    int num = 0;//代表读到多少数据
    while((num = read(fdr,buff,256))>0)
    {   
        write(fdw,buff,num);
    }

    close(fdr);
    close(fdw);

    exit(0);
}
~   

5f00eabf0e5d4213b619b139dec6ba62.png

(4)实现类似命令: 

(cp 文件名 新文件名)

#include<stdio.h>
#include<fcntl.h>
#include<stdlib.h>
#include<unistd.h>

int main(int argc,char *argv[])
{
	if(argc!=3)
    {
        printf("arg error\n");
    }
    char *file_name=argv[1];
    char *newfile_name=argv[2];
    
    int fdr=open(file_nam,O_RDONLY);
    int fdw=open(newfile_name,O_WRONLY|O_CREAT,0600);

	
	if(fdr==-1||fdw==-1)
	{
		exit(0);
	}
	char buff[256]={0};
	int num=0;
	
	while((num=read(fdr,buff,256))>0)
	{
		write(fdw,buff,num);
	}
	
	close(fdr);
	close(fdw);
	
	exit(0);
}

./test03 file.txt newfile.txt

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值