Linux基础与应用开发系列九:各类系统函数

open_close函数

OPEN函数

头文件:

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

函数原型:

  • 当文件存在时

    int open(const char* pathname,int flags)
  • 当文件不存在时

    int open (const char* pathname,int flags,int perms)

返回值

成功:文件描述符

失败:-1

CLOSE函数

头文件:

#include <unistd.h>

函数原型:

int close(int fd)

返回值:

成功:0

失败:-1

程序验证步骤

 程序

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>




int main(int argc,char** argv)

{
	int fd1,fd2;
	char buf[512];
	int read_size;

	if(argc!=3)
	{
		printf("param error!!\r\n");
		return -1;
	
	
	}
	fd1= open(argv[1],O_RDONLY);
	fd2= open(argv[2],O_WRONLY|O_CREAT,0666);

	if (fd1<0||fd2<0)
	{
		printf("open erro!\r\n");
		return -1;
	}
	while (1)
	{
		read_size=read(fd1,buf,512);
		if (read_size==0)
		break;
		write(fd2,buf,read_size);
		
	}
	
   close(fd1);
   close(fd2);
   return 0;


}

赋权限

gec@ubuntu:~/IO_program$ sudo chown -R gec ~/IO_program/part_2

猜你喜欢

转载自blog.csdn.net/qq_51519091/article/details/132196752