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:
- #include <stdio.h>
- #include <stdlib.h>
- int copyFile(char *fileread, char *filewrite);
- int main(){
- Char FileRead[+]; //File name to copy
- Char FileWrite[+]; //The file name after copying
- Get user input
- printf("files to be copied:");
- scanf("%s", FileRead);
- printf("Copy files to:");
- scanf("%s", FileWrite);
- Make a copy operation
- If( copyFile(fileRead, FileWrite) ){
- printf("Congratulations, file copy succeeded!") \ n");
- }Else{
- printf("file copy failed!") \ n");
- }
- return 0;
- }
- /**
- * File copy function
- * @param fileRead The file to be copied
- * @param The save path of the file after FileWrite copy
- * @return int 1: Copy succeeded; 2: Replication Failed
- **/
- int copyFile(char *fileread, char *filewrite){
- FILE *fpread; //point to the file you want to copy
- FILE *fpwrite; //point to the copied file
- int Bufferlen = 1024x768*4; //Buffer length
- Char *buffer = (char*)malloc(Bufferlen); //Open cache
- int Readcount; //number of bytes actually read
- if ( Fpread =fopen (fileRead , "RB" ) == null | | (Fpwrite=fopen (Filewrite, "WB" == null ) {
- printf("Cannot open file, press any key to exit! \ n");
- Getch();
- Exit(1);
- }
- Continuously reads the contents from the Fileread, places it in the buffer, and then writes the contents of the buffer to FileWrite
- While( (readcount=fread(buffer, 1, Bufferlen, Fpread)) > 0 ){
- Fwrite(buffer, Readcount, 1, Fpwrite);
- }
- Free(buffer);
- Fclose(Fpread);
- Fclose(Fpwrite);
- return 1;
- }
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)