Strcat () function FAQs

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.