欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

A copy console applicaton on ubuntu

程序员文章站 2022-06-10 11:11:43
...
#include<iostream>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
using namespace std;
int main(){
	int fd1,fd2;//file descibe
	char buf[1];//buffer
	int rd=0,wr=0;
	fd1=open("//home//hhj//Documents//hello.cpp",O_RDONLY);
	fd2=open("//home//hhj//Documents//a.txt",O_WRONLY);
	if(fd1==-1||fd2==-1){
		cout<<"open error"<<endl;
	}
	else{
		rd=read(fd1,buf,1);
		while(rd!=0){
			if(rd==-1){cout<<"read error"<<endl;break;}
			else{
				wr=write(fd2,buf,1);
				if(wr==-1){cout<<"write error"<<endl;break;}
				else{
					rd=read(fd1,buf,1);
				}
			}
		}

	}
	fd1=close(fd1);
	fd2=close(fd2);
	if(fd1==-1||fd2==-1)cout<<"close error"<<endl;
	return 0;
}

The code above is to copy the file hello.cpp to a.txt.It's only useful for linux OS.