Recently in the content of remote upgrades, through practice to really realize the role of different copy functions
char*strcpy (char *dest, const char *SRC);
It operates on a string, completes a copy from the source string to the destination string, and a segment error occurs when the source string size is larger than the destination string's maximum storage space.
int sprintf (CHAR*STR, const char *format, ...)
The source object of a function operation is not limited to a string: The source object can be a string, or it can be data of any basic type. The main implementation is to convert other data types to strings
void *memcpy (void*dest, const void *SRC, size_t N)
A copy of the memory is implemented, which copies a piece of memory to another piece of memory. The function has no type for the source object and destination object now, just a copy of the memory
However, in software upgrades, it is best to use memcpy to copy data when a data copy is received from a network-delivered upgraded content. Because when strcpy, sprintf is copied, when checking that the source string has ' ' that is, the ASCII code is 00, the data terminator is considered to stop copying
#include <stdio.h> #include <stdlib.h> #include <string.h> int main () {char str1[48];
Char str2[48];
Char str3[48];
Char test[48];
int count = 0;
FILE *FP1 = NULL;
FILE *FP2 = NULL;
FILE *fp3 = NULL;
FILE *FP = NULL;
memset (str1, 0, sizeof (STR1));
memset (str2, 0, sizeof (STR2));
memset (STR3, 0, sizeof (STR3));
fp = fopen ("/usr/local/ctest/memtest.bin", "R");
if (fp = = NULL) {printf ("open source file error \ n");
return-1;
} FP1 = fopen ("/usr/local/ctest/memtest1.bin", "w");
if (FP1 = = NULL) {printf ("Open file1 for strcpy error\n");
return-1;
} FP2 = fopen ("/usr/local/ctest/memtest2.bin", "w");
if (FP2 = = NULL) {printf ("Open file2 for strcpy error\n");
return-1; } FP3 = FOPEn ("/usr/local/ctest/memtest3.bin", "w");
if (fp3 = = NULL) {printf ("Open file3 for strcpy error\n");
return-1;
Fread (test, 1, FP);
Fclose (FP);
printf ("Test is%s\n", test);
strcpy (str1, test);
Fwrite (str1, 1, FP1);
sprintf (str2, "%s", test);
Fwrite (str2, 1, FP2);
memcpy (STR3, Test, 48);
Fwrite (STR3, 1, FP3);
Fclose (FP1);
Fclose (FP2);
Fclose (FP3);
return 0;
}
The above source file Memtest.bin is part of an upgrade file, and there are many ASCII code 00 data in the upgrade file.
Above is a program that first opens the file to read a portion (read 48 bytes here) and store it in test, and then use strcpy, sprintf, memcpy, respectively, to copy to
STR1, STR2, STR3, and write to file Memtest1.bin, Memtest2.bin, Memtest3.bin, and finally compare the 3 files to the Memtest.bin
The above figure is memtest.bin and memtest1.bin contrast, can be found to be different, that strcpy in the process of copying, when the ASCII code is 00 o'clock, it is considered that the end of the data caused.
The above figure is memtest.bin and memtest2.bin contrast, can be found to be different, that sprintf in the process of copying, when the ASCII code is 00 o'clock, it is considered that the end of the data caused.
The above figure is the comparison of Memtest.bin and Memtest3.bin, can be found to be the same, because the memcpy is a copy of memory, so there is no problem above
By comparison, it is found that after the sprintf, strcpy copy of the data has been encountered 00 of the value of the stop copying, resulting in the written file does not match the contents of the source file, if you use these two functions
To copy, the upgrade will cause the system to not boot, and after the memcpy copy of the file Memtest3.bin and the source file is the same, so in the Copy software upgrade content as far as possible
Use memcpy instead of strcpy, sprintf, or you may cause the system to fail to start