Use a regular expression (regex_replace) to simulate reading the INI file, regex_replaceini
If you don't talk much about it, paste the Code directly. The specific code is as follows:
# Include "stdio. h "# include <sstream> # include <iostream> # include <fstream> # include <regex> using namespace std; void Trim (char * str ); void lTrim (char * str); void rTrim (char * str); // This extension function provided by sscanf and the regular expression // sscanf cannot be actually called a regular expression, because his writing is still inseparable from % // [] indicates the character range, {} indicates the number of repetitions, ^ indicates taking non, * Indicates skipping. Therefore, the above url can be parsed as follows: // char url [] = "dv: // 192.168.1.253: 65001/1/1" /// sscanf (url, "% [^: //] % * c % [^:] % * c % d ", protocol, ip, port, chn, type); /// explain // obtain the longest string first, but it does not include the string ://, so protocol = "dv \ 0"; // then skip three characters, (% * c) is actually skipping: // then take a string that does not include a string :, so ip = 192.168.1.253 is simplified here. The IP address is used as a string, and no check is performed. // skip the colon to get the port, and then skip/get the channel number to chn, skip or extract the stream type to type. // C language implementation example void test1 () {char url [] = "dv: // 192.168.1.253: 65001/1/1"; char protocol [10]; char ip [17]; int port; int chn; int type; sscanf (url, "% [^: //] % * c % [^:] % * c % d ", protocol, ip, & port, & chn, & type); printf (" % s, % s, % d, % d, % d \ n ", protocol, ip, port, chn, type);} // read a string in ini and get: hello world! // Normal string 1:-claim = "hello world! "// Normal string 2: claim =" hello world! "// Normal string 3: claim =" hello world! "// Normal string 4: claim _ = hello world! // Interference string 1: cl-aim = \ "hello world! "// Interference string 2: clai3m =" hello world! \ "// Interference string 3: cla_im = \" hello world! \ "// Interference string 4: claim = '" hello world! \ "// Interference string 5: claim = @" \ nhello world! "// Interference string 6: claim = L" hello world! "// Unprocessed 1: claim [1] = 1 // unprocessed 1: claim [2] = 1 void test2 () {char line [1000] = {0 }; char val [1000] = {0}; char key [1000] = {0}; FILE * fp = fopen ("1.txt"," r "); if (NULL = fp) {printf ("failed to open 1.txt \ n"); return;} while (! Feof (fp) {memset (line, 0, sizeof (line); fgets (line, sizeof (line)-1, fp ); // contains \ n printf ("% s", line); Trim (line) of each line; // extracts the content before the equal sign memset (key, 0, sizeof (key); // The format used by sscanf is not a regular expression and \ s cannot be used to represent various blank characters, that is, spaces or \ t, \ n, \ r, \ f sscanf (line, "% [^ \ t \ n \ r \ f =]", key); // sscanf (line, "% * [^ a-zA-Z0-9 _-] % [^ \ t \ n \ r \ f =]", key); printf ("key: [% s] \ n ", key); // extract the content memset (val, 0, sizeof (val) after the equal sign; sscanf (line, "% * [^ =] % * C % [^ \ n] ", val); // The linefeed Trim (val); printf (" val: [% s] \ n ", val); // remove double quotation marks on both sides //... // Insert map // map [key] = value; // string to another type // atoi, atol, atof} printf ("\ n "); fclose (fp);} // The C ++ implementation template of the above example <class T1, class T2> inline T1 parseTo (const T2 t) {static stringstream sstream; T1 r; sstream <t; sstream> r; sstream. clear (); return r;} void test3 () {char val [1000] = {0}; char ke Y [1000] = {0}; ifstream fin ("1.txt"); string line; if (fin) {while (getline (fin, line )) // line does not contain the line break {cout <line <endl; // extract the content before the equal sign // 1st sets () to indicate any space characters, 2nd group () indicates a word (can contain _ or-), // 3rd group () indicates more than 1 space character (OR = ), end with any string regex reg ("^ ([\ s] *) ([\ w \-\ _] +) ([\ s =] + ). * $ "); // take the 2nd group instead of the original string key = regex_replace (line, reg," $2 "); cout <" key: {"<key <"} "<endl; // content after the equal sign is extracted // 1st groups () indicate any space characters, 2nd Group () indicates a word (can contain _ or-), // group 3rd () indicates more than one space character (OR =), and group 4th () indicates any character, // a group of 5th () indicates that it ends with any space character (or carriage return linefeed. Reg = regex ("^ ([\ s] *) ([\ w \-\ _] +) ([\ s =] + )(. *) ([\ s \ r \ n] *) $ "); // replace string val = regex_replace (line, reg, "$4"); cout <"val: {" <val <"}" <endl; // remove double quotation marks on both sides //... // Insert map // map [key] = value; // string to another type // int I = parseTo <int> ("123 "); // float f = parseTo <float> ("1.23"); // string str = parseTo <string> (123 );}} else // This file does not exist {cout <"no such file" <endl ;}} void mai N () {// test1 (); test2 (); test3 ();} void lTrim (char * str) {int I, len; len = strlen (str ); for (I = 0; I <len; I ++) {if (str [I]! = ''& Str [I]! = '\ T' & str [I]! = '\ N' & str [I]! = '\ R' & str [I]! = '\ F') break;} memmove (str, str + I, len-I + 1); return;} void rTrim (char * str) {int I, len; len = strlen (str); for (I = len-1; I> = 0; I --) {if (str [I]! = '') & (Str [I]! = 0x0a) & (str [I]! = 0x0d) & (str [I]! = '\ T') & (str [I]! = '\ F') break;} str [I + 1] = 0; return;} void Trim (char * str) {int I, len; // first remove the left space len = strlen (str); for (I = 0; I <len; I ++) {if (str [I]! = ''& Str [I]! = '\ T' & str [I]! = '\ N' & str [I]! = '\ R' & str [I]! = '\ F') break;} memmove (str, str + I, len-I + 1); // remove the right space len = strlen (str ); for (I = len-1; I> = 0; I --) {if (str [I]! = ''& Str [I]! = '\ T' & str [I]! = '\ N' & str [I]! = '\ R' & str [I]! = '\ F') break;} str [I + 1] = 0; return;}/* void Trim (char * str) {lTrim (str ); rTrim (str );}*/
The above section describes how to use a regular expression (regex_replace) to simulate reading the INI file. I hope it will help you. If you have any questions, please leave a message, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!