Two functions that are easily ignored in CC ++ development

xiaoxiao2021-03-05  29

Oh, the title is somewhat, maybe someone else does not need my extra remind. But I will think about it. I have made the mistakes that others will be committed, so I still have to go, don't sell Guan Zi, these two The function is "sprintf" and "sscanf", which has been influenced by these two functions, please leave, stay away. Due to the impact of advanced languages, the expansion of the MFC, STL's C class library, and human habits are familiar with themselves Method means to solve the impact of problem habits, some very useful C basic functions are often forgotten, in many occasions, there is no way to replace, but will fall in special occasions, and write functions, it is not convenient Not safe, there is danger that causes unnecessary losses, so I am remind you to pay attention to the use of "sprintf" and 'sscanf ". 1. Function sprintf: Output parameters in fixed format to the character array, this The array is in the end of the empty end, and the Sprintf does not border check if the Sprintf () generated output is greater than the capacity of the array. Example: Place "One A 10" CHAR STR [255] in STR [255]; Sprintf (Str, " % S% C% D "," One ", 'A', 10); 2. Function SSCANF: Facts with the sprintf, it is to read the string in the array to the variable in fixed format, return value It is the number of variables that actually assigns, which does not include the domain skipped, and zero represents unfielded domain. EOF indicates that the first assignment has been wrong. Example: read from the string array "One A 10" read into a string Variables, a character variable, a integer variable. Char strsrc [] = "one a 10"; char strdest [6]; char cdest; int NDest; SSCANF ("One A 10", "% s% C% D "StrDest, & cdest, & ndest); After a string variable, a character variable, a constitutive variable will take the value of" one ", 'a', 10. These two functions are integrated in the string and Decomposition will play a big role, absolutely not on the format method of the CSTRING class, you can try me to say that it is true.

转载请注明原文地址:https://www.9cbs.com/read-33999.html

New Post(0)