Simple implementation of the file copy command CP

Source: Internet
Author: User
Tags file copy

文件复制在程序中经常会用到,不过最主要的是对文件的操作。
/* the function of this program is "**.exe srcfile desfile", that is, srcfile copy to Desfile, so when running the program must be added after two parameters, the first    is the path to a file that already exists, and the other is the path to which it needs to be copied.    This program mainly utilizes the FGETC () function and the FPUTC () function in the file manipulation function.            Function Description: fgetc (FILE *FP): reads a character from the stream without completing a character the FP pointer automatically points to the next character. EOF is returned when an error is read.        You can determine whether the current file is finished reading by the return value. FPUTC (char ch, FILE *FP): Enter a character into the stream, similar to fgetc, after each write one the FP points to the next, waits for the input to continue, or the stream closes in the function we also need to use the fopen (cha R * FileName, Char *type), the first parameter is the file path we need, and the second parameter is the way to read it.        Read as follows: R: Read-only open a text, only read data w: Write only open or create a text file, only allow write data a: Append open a text file, and write data at the end of the file RB: Read-only open a binary file, read-only        WB: Write only open, or create a binary file, only allow write data ab: Append open a binary file, and write data at the end of the file r+: Read and write open a text file, allow write and read w+: Read and write open or create a text file, can read and write A +: Read and write open a text file, allow reading, or append data to the end of the file rb+: Read and write open a binary file that allows read and write wb+: reads and writes open or create a binary file, allowing read and write ab+: Read and write open a binary file, allow read, or append data to the end of the file and a second function, fclose (file *FP), which is important to close the stream of files. */#include <stdio.h>//In this header file contains the various file manipulation functions we need intMainintargcChar* argv[]) {FILE *srcfile, *desfile;//Define two file pointers    if(ARGC! =3)//Determine if the parameter input meets the requirements{printf("Please input <%s srcfile desfile>", argv[0]);//This place prompts the user program to execute the correct way        return 0; } srcfile = fopen (argv[1],"RB");//Open the source file in a binary-readable way    if(Srcfile = = NULL) {printf("Source File open failure!!");return 0; } desfile = fopen (argv[2],"WB");//Open the destination file in binary mode    if(Desfile = = NULL) {printf("Destination File open failure!!");return 0; }/ * Start copy * /     while(!feof (Srcfile))//Determines whether the current pointer is EOF{PUTC (getc (srcfile), desfile);    } fclose (Srcfile); Fclose (Desfile);return 0;}

Simple implementation of the file copy command CP

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.