Two simple methods of copying files in C language [fundamentally solve the problem], C Language
There are several methods on the Internet:
1. Use the file copy API provided by the operating system
2. Use the copy file function provided by C language itself
3. Directly read and write files and operate them from the file perspective, so as to directly copy a file
Here we use this third type.
The idea of copying a file is like this. For text type, you can directly read and write the file to another file by using the character-based read/write method. But how should I operate non-hierarchical files? My method is to open the file in binary format, and then perform operations on the byte in sequence, and read and write the file from the first byte to the last byte in sequence to replicate the file. Therefore, we can obtain the following information, this method can also be used for remote operations, that is, remote file transmission.
So the question is, how can we read files in bytes? Here I have summarized two methods.
Note: The solution in this article is to read data by byte. If you think the efficiency is low, you can read data by multiple bytes. However, there is a problem, how can we ensure that the size of the source file is the same as that of the source file, rather than the size of the source file. We recommend that you first obtain the size of the source file and then directly read it for operation.
In the C language system, void pointer occupies one byte, and char occupies one byte, which meets our requirements. Therefore, we can use them separately to complete the required operations.
Note that the concept we use here is that the type does not limit the data type used, not that the char type can only be used to store the char type variables, data Types provide a series of operations for specific types, which facilitates our operations, but there cannot be a mindset. A data type provides a possibility, for example, the char variable gives us 2 ^ 8 possibilities. If we need to represent 16 possibilities, we can use a char variable to store it, instead of using an int-type variable, the system overhead is saved. However, in actual situations, we need to consider the program's ease of use, rather than the memory issue. What's more, the memory is getting cheaper, the space is getting bigger and bigger, and the CPU performance is getting better and better.
The source code is as follows:
1 # include <stdio. h> 2 3 int main (int argc, char * argv []) 4 5 {6 7 FILE * op, * indium; 8 9 op = fopen ("a.png ", "rb"); 10 11 indium = fopen ("B .png", "wb"); 12 13 void * buf; 14 15 char c; 16 17 while (! Feof (op) 18 19 {20 21 // method 2 22 23 fread (& buf, 1, 1, op); 24 25 fwrite (& buf, 1, 1, P ); 26 27 28 29 // method 2 30 31 fread (& c, 1, 1, op); 32 33 fwrite (& c, 1, 1, P ); 34 35} 36 37 fclose (indium); 38 39 fclose (op); 40 41 printf ("over"); 42 43 return 0; 44 45} 46 47
Record it and keep it for future reference. It is also convenient for others.
Thank you for your support!
You can contact me. Renhanlinbsl@163.com
2016.1.30
22: 26