How to use the special symbol tab and newline symbol in C++
This article shows you the use of C++ special symbols tab and newline symbols, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.
Foreword:
We often encounter some Linux kernel information needs, such as a wifi data, separated by a tab keyboard, and then each line is distinguished by a newline character, as shown below
The first location is: wifi name, the second location is: signal strength, the third location is: signal type 2.4Ghz or 5Ghz, the fourth location is: encryption type, the fifth type is: MAC address.
When we encounter such data, the common C function parsing is to use the while loop to match the'\ t'(tab key) and'\ n' (newline symbol) in the string, and then use the str function in the C standard library, such as strncpy to copy or strstr to find it, similar to the following:
Int I, j; I = 0; j = strlen (strIn)-1; while (strin [I] = =''| strIn [I] = ='\ t') + I; while (strin [j] = ='\ t')-- j; strncpy (strOut, strIn + I, j-I + 1)
So how do we use C++ to parse it? recently, we just parsed some of the wifi information, so I'd like to share this part with you.
Parse the code:
Corresponding to the table with the wifi information above, we see that there are five data in a row, and the package receives a wifi message of type string.
We should pay attention to three things:
Let's first confirm the scope of parsing, that is, know the first bit and the last bit, set up recivemsg.begin and recivemsg.
Segment after identifying tab symbols and newline symbols
The divided data is stuffed into a vector container, and then because there are five data in each row, we can distribute the data according to a set of 05s.
The actual code is as follows:
Vector Parse_Wifi_List (string& msg) {vector words; if (msg.empty ()) return words; string::iterator temp_p = msg.begin (); string sepword; bool bit_true = false While (temp_p! = msg.end ()) {if (/ * * temp_p = =''| | * / * temp_p = ='\ t' | | * temp_p = ='\ n') {if (bit_true) {words.push_back (sepword) PP_INFO ("seword:% s", sepword.c_str ()); sepword.clear ();} bit_true = false; temp_p++; continue;} else {bit_true = true Sepword + = * temp_p;} if (* temp_p + + = ='0') {break;} / / else / / {/ / PP_INFO ("% d% d% c", msg.end (), * temp_p,*temp_p) / /}} / / for (auto point: words) / / {/ / printf (".% s\ n", point.c_str ()); / /} / / for (uint16_t I = 0polii