Strcat () function FAQs
The strcat (char * _ Destination, const char * _ Source) function is used to paste the last string to the end of the previous string.
Prototype
Char * strcat (char * _ Destination, const char * _ Source)
Common Errors
A common error in strcat functions is that the array is out of bounds, that is, after two strings are connected, the length exceeds the length defined by the first string array, resulting in out-of-bounds
Example1:
1 void charWrite () {2 FILE * file; 3 char type [4] = "wt +"; 4 char path [30] = "C: /Users/Fahy/Desktop/"; // The total length of the array is 30 characters, and the initial storage is 22 characters 5 char filename [20], ch; 6 scanf ("% s", filename); // if there are more than 8 characters, strcat will cross the line 7 ch = getchar () when combining the two strings (); 8 ch = getchar (); 9 strcat (path, filename); 10 if (! (File = fopen (path, type) {11 printf ("Can't open this file \" % s \ "", path ); 12 system ("pause"); 13} 14 else {15 while (ch! = EOF) 16 {17 fputc (ch, file); 18 ch = getchar (); 19} 20} 21 fclose (file); 22}
Solution
You can only define the length of the first parameter.