The C language implements the file copy function (including text files and binaries)

Source: Internet
Author: User
Tags file copy fread

Copying files is a common feature that requires writing a piece of code, letting the user enter the file to be copied and the new file, and then copying the file. Files that can be copied include text files and binaries, you can copy a 1G movie, or you can copy a 1Byte txt document.

The main idea of implementing file replication is to create a buffer that reads the contents from the original file to the buffer, and writes the contents of the buffer to the newly created file after each read, until the contents of the original file are read.

Here are two key issues to address:
1) How large is the buffer suitable for? If the buffer is too small, it will increase the number of read and write, too large can not significantly improve efficiency. At present, most of the disk sector is 4K aligned, if the data read and write is not an integer multiple of 4K, will be read across sectors, reduce efficiency, so we open 4K buffer.

2) The data in the buffer is not the end flag, and if the buffer population is not satisfied, how to determine the number of bytes written? The best way to do this is to return the number of bytes read to each read.

The prototype for Fread () is:

size_t fread (void *ptr, size_t size, size_t count, FILE *FP);

It returns the number of blocks successfully read and written, which is less than or equal to count. If we let the parameter size be equal to 1, then the number of bytes read is returned.

Note: fopen () must open the file in binary form, not open as text, otherwise the system will do some processing of the file, if it is a text file, such as. txt, may not be a problem, but if it is a file in other formats, such as. mp4,. rmvb,. jpg, etc., the copy will go wrong. cannot be read.

Code implementation:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int copyFile(char *fileread, char *filewrite);
  4. int main(){
  5. Char FileRead[+]; //File name to copy
  6. Char FileWrite[+]; //The file name after copying
  7. Get user input
  8. printf("files to be copied:");
  9. scanf("%s", FileRead);
  10. printf("Copy files to:");
  11. scanf("%s", FileWrite);
  12. Make a copy operation
  13. If( copyFile(fileRead, FileWrite) ){
  14. printf("Congratulations, file copy succeeded!") \ n");
  15. }Else{
  16. printf("file copy failed!") \ n");
  17. }
  18. return 0;
  19. }
  20. /**
  21. * File copy function
  22. * @param fileRead The file to be copied
  23. * @param The save path of the file after FileWrite copy
  24. * @return int 1: Copy succeeded; 2: Replication Failed
  25. **/
  26. int copyFile(char *fileread, char *filewrite){
  27. FILE *fpread; //point to the file you want to copy
  28. FILE *fpwrite; //point to the copied file
  29. int Bufferlen = 1024x768*4; //Buffer length
  30. Char *buffer = (char*)malloc(Bufferlen); //Open cache
  31. int Readcount; //number of bytes actually read
  32. if (  Fpread =fopen (fileRead ,  "RB" )   == null | |   (Fpwrite=fopen (Filewrite,  "WB" == null ) {
  33. printf("Cannot open file, press any key to exit! \ n");
  34. Getch();
  35. Exit(1);
  36. }
  37. Continuously reads the contents from the Fileread, places it in the buffer, and then writes the contents of the buffer to FileWrite
  38. While( (readcount=fread(buffer, 1, Bufferlen, Fpread)) > 0 ){
  39. Fwrite(buffer, Readcount, 1, Fpwrite);
  40. }
  41. Free(buffer);
  42. Fclose(Fpread);
  43. Fclose(Fpwrite);
  44. return 1;
  45. }

Operation Result:

Files to copy: D://1.mp4 Copy the file to: D://2.mp4 Congratulations, the file copy is successful!

If the file does not exist, a prompt is given and the program is terminated:

Files to copy: D://123.mp4 copy files to: D://333.mp4d://cyuyan.txt:no such file or directory


Line 46th is the core code for File replication. With the Fread () function, each time a Bufferlen byte is read from the FileRead file, put into the buffer, and the contents of the buffer are written to the FileWrite file through the fwrite () function.

Normally, Bufferlen bytes are read every time, or Readcount=bufferlen, if the file size is less than Bufferlen bytes, or if it reads to the end of the file, the actual bytes read will be less than Bufferlen, which is Readcount <bufferlen. Therefore, when writing files through Fwrite (), the readcount should prevail.

The C language implements the file copy function (including text files and binaries)

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.