question archive write a program that emulates the DOS COPY command
Subject:Computer SciencePrice:2.87 Bought7
write a program that emulates the DOS COPY command. That is, it should copy the contents
of a text file (such as any .txt file) to another file. Invoke the program with two user inputs—the source
file and the destination file names and complete path.
check that the user has typed the correct path and file names, and that the files specified can be opened.
Answer: #include<iostream> #include<fcntl.h> #include<unistd.h> #include<cstdlib> using namespace std; int main(int argc,char* argv[]) { if(argc<2) { cout<<"Enter the name of source & destination file[with path] in command line\n"; exit(1); } char* src=argv[1],*dest =argv[2]; int bufferSize=512,count=0,fd1,fd2; char buffer[bufferSize]; fd1=open(src,O_RDONLY); fd2=open(dest,O_CREAT | O_WRONLY); if(fd1<0) { cout<<"Unable to open source file\n"; exit(1); } do { count=0; count=read(fd1,buffer,bufferSize); if(count>0) write(fd2,buffer,count); else if(count<0) { cout<<"Reading error!\n"; exit(1); } }while(count); cout<<"Successful file transfer\n"; close(fd2); close(fd1); return 0; }
Use following format to compile and run the above code.
compile:
g++ filename.cpp
run:.
/a.out <src_file_name> <path>
(e.g. ./a.out a.txt /home/Desktop/b.txt)