An error occurred while compiling a code about file copy operations implemented using the C library function. The root cause of the error is to take it for granted that the fread function is used and does not have a deep understanding of it. Later I found the error with the help of a netizen.
In fact, the usage of functions can be assisted by man in Linux.
For example, fread. Type
man 3 fread
This will show the following:
NAME fread, fwrite - binary stream input/outputSYNOPSIS #include <stdio.h> size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream); size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);DESCRIPTION The function fread() reads nmemb elements of data, each size bytes long, from the stream pointed to by stream, storing them at the location given by ptr. The function fwrite() writes nmemb elements of data, each size bytes long, to the stream pointed to by stream, obtaining them from the location given by ptr. For nonlocking counterparts, see unlocked_stdio(3).RETURN VALUE On success, fread() and fwrite() return the number of items read or written. This number equals the number of bytes transferred only when size is 1. If an error occurs, or the end of the file is reached, the return value is a short item count (or zero). fread() does not distinguish between end-of-file and error, and callers must use feof(3) and ferror(3) to determine which occurred.
Focus on return values.
We can see that if the call is successful, the function returns the number of elements read. If you want to return the actual number of bytes read, only when size = 1, that is, the second parameter size is 1. If a read error occurs, or the object has reached the end of the file, the returned value is a small element value or 0. The following code is provided to correctly implement the file copy code for analysis.
# Include <stdio. h> # define buffer_size 1024int main (INT argc, char * argv []) {file * from_fp, * to_fp; int bytes_read, bytes_write; char * PTR; char buffer [buffer_size]; if (argc! = 3) // parameters include the source file name and target file name {printf ("input failed! \ N "); return 1;} If (from_fp = fopen (argv [1]," R ") = NULL) // open the source file {printf ("file is not exist \ n"); return 1;} If (to_fp = fopen (argv [2], "W +") = NULL) // open the second file {printf ("Open File failed! \ N "); return 1;} while (bytes_read = fread (buffer, 1, buffer_size, from_fp) // read buffsize byte {If (bytes_read> 0) // read valid data {PTR = buffer; while (bytes_write = fwrite (PTR, 1, bytes_read, to_fp) // write data to the target file {If (bytes_write = bytes_read) // break; else if (bytes_write> 0) // {PTR + = bytes_write; bytes_read-= bytes_write;} If (bytes_write = 0) // write error break;} fclose (from_fp); fclose (to_fp); Return 0 ;}
Note that the second parameter size in fread and fwrite is 1, so that the returned value is the actual number of bytes read or written. At the beginning, the program I wrote was not like this. I am
while(bytes_read = fread(buffer, BUFFER_SIZE, 1,from_fp))
while(bytes_write = fwrite(ptr, bytes_read, 1, to_fp))
Here, the third parameter is 1. In other words, to read one element, this element contains buffer_size bytes. Because my file does not meet this condition, this element value does not exist. The returned value is 0, which is why my file data is not copied to another file, because the code in this loop is not executed at all.
Another issue to be noted is that the fread function cannot distinguish between the end of a file and the error. Therefore, the ferror () and feof () functions must be used to determine the exact situation. Therefore, there is another way to write about file replication. The Code is as follows:
# Include <stdio. h> # include <string. h> # define buffer_size 1024int main (INT argc, char * argv []) {file * from_fp, * to_fp; // int bytes_read, bytes_write; long file_len = 0; char buffer [buffer_size]; If (argc! = 3) // parameters include the source file name and target file name {printf ("input failed! \ N "); return 1;} If (from_fp = fopen (argv [1]," R ") = NULL) // open the source file {printf ("file is not exist \ n"); return 1;} If (to_fp = fopen (argv [2], "W +") = NULL) // open the second file {printf ("Open File failed! \ N "); return 1;} fseek (from_fp, 0l, seek_end); // locate the file pointer to the end of file_len = ftell (from_fp ); // obtain the file length fseek (from_fp, 0l, seek_set); // locate the file pointer to the start position while (! Feof (from_fp) // judge whether the file ends {fread (buffer, buffer_size, 1, from_fp); If (buffer_size> file_len) // The buffer length is greater than the file length fwrite (buffer, file_len, 1, to_fp); else {fwrite (buffer, buffer_size, 1, to_fp); file_len-= buffer_size;} bzero (buffer, buffer_size ); // clears the buffer} fclose (from_fp); fclose (to_fp); Return 0 ;}
Here there are several more functions, and feof is the function used to check whether the file ends. If the return value is not 0, the end of the file is displayed. Another function, bzero, is used to clear a buffer. It must contain the header file string. h. Note that the fread and fwrite functions are written in the format of the third parameter 1.