How to use c language to implement mv command like linux mv
Principle:
Open two files, read the contents of the first file, write to the second file, and finally delete the first file:
#include<sys/types.h> #include<sys/stat.h> #include<stdio.h> #include<fcntl.h> main( int argc,char *argv[] ) { int i,fd1,fd2; char *file1,*file2,buf[2]; file1=argv[1]; file2=argv[2]; printf("file1=%s file2=%s",file1,file2); fd1=open(file1,O_RDONLY,0777); fd2=creat(file2,0777); while(i=read(fd1,buf,1)>0) write(fd2,buf,1); remove(file1); // close(fd1); close(fd2); }Source:al.wwwutturi.com