How to safely format strings

xiaoxiao2021-03-06  60

In the C language, we want to format the output, usually we string with Sprintf.

For example: char Temp [100]; Sprintf (TEMP, "% s", "helloworld");

Normally, there should be no problems. However, in some cases, it is intentionally unexpected results. For example: When the error information is performed, we take the following way:

Char Temp [64]; Sprintf (Temp, "Error At File% S, LINE% D, Column% D, Message:% S", Strfile, Linenumber, ColumnNumber, StrMessage;

In general, if the error message is small, there is no problem with the above program. If the error message is very long, the above data will be offline, resulting in rewriting the content in other memory, causing some errors you imagined. At this time, it is estimated that your solution is to increase the space of TEMP, such as TEMP [1000]. That's right, after this correction, the probability that caused the above error will be less, but it has not yet been completely resolved. If the user provides more error messages, there is still a problem. So do you have any way to completely solve this problem? ? ? ?

In C , there is a String class that can be used to dynamically store strings, which adjust its length according to the length of the current string. And string.begin () is equivalent to the char * pointer.

Speaking of this, do you have an impulse to rewrite the above program? (I so).

String straTemp; Strtemp.Rserve (64); // Optimize String Memory Allocation Policy Sprintf (Strtemp.Begin (), "Error At File% S, LINE% D, Column% D, Message:% S", Strfile, LineNumber , ColumnNumber, StrMessage;

Compile through, ^ _ ^ (hacking)!

Test it, when a shorter error message (no more than 64), there is no problem, but after more than 64, it will make an error! ! ! (Is it more than the above more 'security'?)

Why will it be wrong above? ? ? Is String not dynamically expanded storage space?

That's right, if more than 64, String will perform dynamic allocation space, because String generally ensures its storage spatial continuity, so it is reassigned a large enough space, then the data COPY, this process is similar to Realloc to assign space. This will inevitably lead to the current String.begin () and the address pointed to by the previous begin (). The address of our sprintf points is the previous string.begin (), not the space after reassign, the generally allocated space will be released, so it will cause the program to errors.

Which do we have any other way to improve the above problems? ? ?

The CSTRING class in the MFC has a member function Format to format strings and can be dynamically extended. Is it cool? ? ?

However, for me, anything is not in the UI place (ie, the interface display section), try to avoid the content of the MFC, so that the future transplant is more convenient (maybe a bit of worrying, there is no other solution) ? ? ? After all, the formatted string is very common, and there is a specific function to complete (Sprintf). Is there a solution in C ?

Remember that in the IO output stream of C , the standard COUT can be used to format the output to the screen. If FSTREAM can be used to format the output to the file. (To say this, I seem to have a string stream?)

Hurry up with MSDN search, huh, I found this guy.

No longer said, directly sample.

Std :: ostringstream strout (iOS_BASE :: Out);

Strout << "Error At File" << File << "Line" << LINENUMBER << "Column" << ColumnNumber << Endl; strout << "Message:" << CerrorMsg (ErrorCode) .GeterrorMSG () << Endl; strout << "UserMessage:" << Errstr << ENDL;

Then we can extract the above string. String strout = strout.STR ();

In this step, you should know how to use the String type value.

Oh, I finally solved it, and solved so thoroughly (, don't tell others, tell you a secret, I have encountered this problem, but I have no good to solve, just increase the array of TEMP, The probability of an error will be reduced!).

what? ? ? You can't compile? ? ? ? Can you join the relevant header file? ? ? I don't know which header file, then find it in MSDN. What are you not found? Patience will have it (but in a relatively hidden place).

Use to work with objects of type baras_string. Use Work with char *, Which Are C strings

That is, in the above, we can add the following sentence: #include

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

New Post(0)