C language implements file copy function (including text files and binary files)

Copying of files is a common function, which requires writing a piece of code, allowing the user to input the file to be copied and the newly created file, and then copying the file. The files that can be copied include text files and binary files. You can copy 1G movies or 1Byte txt documents.

The main idea of ​​realizing file copying is to open up a buffer, continuously read the content from the original file to the buffer, and write the content in the buffer to the newly created file after each reading, until the content of the original file is read. Finished reading.

There are two key issues to be solved here:
1) How big is the appropriate buffer to open up? If the buffer is too small, the number of reads and writes will increase, and if the buffer is too large, the efficiency cannot be significantly improved. At present, the sectors of most disks are 4K aligned. If the read and write data is not an integer multiple of 4K, it will be read across sectors, reducing efficiency, so we open up a 4K buffer.

2) The data in the buffer has no end mark. If the buffer is not full, how to determine the number of bytes written? The best way is to return the number of bytes read for each read.

The prototype of 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 make the parameter size equal to 1, then what is returned is the number of bytes read.

Note: fopen() must open the file in binary form, not in text form, otherwise the system will perform some processing on the file, if it is a text file, such as .txt, etc., there may be no problem, but if it is a file in other formats , like .mp4, .rmvb, .jpg, etc., will make an error after copying and cannot be read.

Code:

 
  
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int copyFile(char *fileReadchar *fileWrite);
  4. int  main () {
  5. char fileRead [ 100 ];  // filename to copy
  6. char fileWrite [ 100 ];  // copied file name
  7. // get user input
  8. printf ( "File to copy:" );
  9. scanf("%s", fileRead);
  10. printf ( "Copy file to: " );
  11. scanf("%s", fileWrite);
  12. // do the copy operation
  13. ifcopyFile(fileRead, fileWrite){
  14. printf ( "Congratulations, the file was copied successfully! \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 copy
  23. @param  fileWrite save path of the copied file
  24. @return  int 1: Copy succeeded; 2: Copy failed
  25. **/
  26. int copyFile(char *fileReadchar *fileWrite){
  27. FILE *fpRead // point to the file to copy 
  28. FILE *fpWrite // point to the copied file 
  29. int bufferLen  1024 * 4 // buffer length
  30. char  *buffer  ( char *) malloc (bufferLen );  // open up the buffer
  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 read the content from fileRead, put it in the buffer, and then write the content of the buffer to fileWrite
  38. while(readCount=fread(buffer1, bufferLen, fpRead)) ){
  39. fwrite(buffer, readCount1, fpWrite);
  40. }
  41. free(buffer);
  42. fclose(fpRead);
  43. fclose(fpWrite);
  44. return 1;
  45. }

operation result:

File to copy: d://1.mp4
Copy the file to: d://2.mp4
Congratulations, the file was copied successfully!

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

File to copy: d://123.mp4
Copy the file to: d://333.mp4
d://cyuyan.txt: No such file or directory


Line 46 is the core code for file copying. Through the fread() function, each time bufferLen bytes are read from the fileRead file, put into the buffer, and then the content of the buffer is written into the fileWrite file through the fwrite() function.

Under normal circumstances, bufferLen bytes are read each time, that is, readCount=bufferLen; if the file size is less than bufferLen bytes, or the end of the file is read, the actual bytes read will be less than bufferLen, that is, readCount< bufferLen. So when writing files through fwrite(), readCount should prevail.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325880175&siteId=291194637