cp&tar& using C language program to realize CP command effect

Source: Internet
Author: User

1.CP (copy) file path to copy already exists

The code to implement the CP command is as follows:

  2#include <stdio.h>3   4 //because you want to get two paths in the command, use the two arguments of the main function  5 intMainintargcChar**argv) {  6   7  Charbuf[ -]={0}; 8   9 intSize=0; Ten /*int buf[100]={0}; represents a single store in an array of four bytes. If the data we took from the original file > is 3 bytes, then we can't put it in.*/  OneFILE * p_src=null,*p_dest=NULL; A   - if(ARGC <3){  -printf"format command error \ n");  the         return 0;  - }  -   - //the idea of copying 1: Read the original file into memory, and then write the new file. The original file may be very large.   + //the idea of copying 2: Ants move  -P_src=fopen (* (argv+1),"RB");  +  //argv represents an array of pointers,  A //the first pointer in the pointer array is the./out file, and the second pointer is the original file path  at   - //case of branch handling open failure  - if(!p_src) {  -printf"original file open failed \ n");  - return 0;  - }   in                                                                               -P_dest=fopen (* (argv+2),"WB");  to   + if(!p_dest) {  -printf"new File open failed \ n");  theFclose"p_src");  *P_src=NULL; $ return 0; Panax Notoginseng }  -   the  while(1){  +Size=fread (BUF,sizeof(Char), -, P_SRC);  A if(!size) { Break;}  theFwrite (BUF,sizeof(Char), size,p_dest);  + }  - //How many data you get from the original file, and how many numbers you write.   $Fclose"p_dest");  $p_dest=NULL; -    -Fclose"p_src");  theP_src=NULL; -  Wuyi   the return 0;  -}
View Code

2.tar command

Use the ZCVF option to package (package multiple files into a single compressed file).

Use the ZXVF option to restore (Restore a large package of compressed file content to a normal file).

Error Correction: line 22nd of the code should be./a.out file

cp&tar& using C language program to realize CP command effect

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.