Organizer: finallyliuyu
If the NLP investigator or developer chooses the C ++ language as the development tool. First, we need to overcome two difficulties.
The first challenge is to learn to use regular expressions in C ++. Since C ++ itself does not support regular expressions, we need to use the RegEx sub-library in third-party libraries boost. There are many ways to install boost on the network. For example, some methods are used to describe how to partially install the boost library. Here I have also sorted out how to fully install the boost Library: I have tried c ++ to install boost. The above method is feasible. However, you need to install python26 before installing the boost library.
The second problem is: wide and narrow String Conversion. I have just sorted out some solutions. New summaries will be added in the future. This blog post provides some knowledge to address the second challenge. The following blog posts are divided into two parts: the first part describes the String Conversion required for string processing using the boost regular expression library; the second part describes: path string conversion problems when traversing folders with Chinese characters in the path.
Part 1: String Conversion required for string processing using the boost Regular Expression Library
If the string contains Chinese characters, the regular expression type should be boost: wregex;
Also convert string from string to wstring
String-> wstring: The general idea is to convert string to a string in the C format using c_str (), and then convert the string of the char type into a string of the wchar_t type, then the string of wchart_t is converted into a wstring.
String-> wstringCodeAs follows:
****************************************: Converts a narrow character to a wide character, string-> wstring *//********************************* ***************************************/ wstring mymultibytetowidechar (string sresult) {int iwlen = multibytetowidechar (cp_acp, 0, sresult. c_str (), sresult. size (), 0, 0); // calculate the length of the width string after conversion. (Excluding the string Terminator) wchar_t * lpwsz = new wchar_t [iwlen + 1]; multibytetowidechar (cp_acp, 0, sresult. c_str (), sresult. size (), lpwsz, iwlen); // formally convert. Lpwsz [iwlen] = L' \ 0'; wstring wsresult (lpwsz); Delete [] lpwsz; return wsresult ;}
The function calls the multibytetowidechar function in windows. h to convert a char string to a wchar_t string.
In order to display the string after regular expression processing on the console, wstring-> string
The Code is as follows (functions in windows. h are also used in the Code ):
/*************************************** ********************************* // * Convert a wide string use a narrow string to output *//******************************** **************************************** /string mywidechartomultibyte (wstring wsresult) {string sresult; int ilen = widechartomultibyte (cp_acp, null, wsresult. c_str (),-1, null, 0, null, false); // calculate the length of the converted string. (Including string Terminator) char * lpsz = new char [ilen]; widechartomultibyte (cp_oemcp, null, wsresult. c_str (),-1, lpsz, ilen, null, false); // convert the string formally. Sresult. Assign (lpsz, iLen-1); // assign a value to the string object. Delete [] lpsz; return sresult ;}
Part 2:
Path string conversion problems when traversing folders with Chinese characters in the path.
First, the test code is provided:
Int _ tmain (INT argc, _ tchar * argv []) {int end; findfile ("E: \ old computer"); cout <"finish" <Endl; cin> end ;}
The folder is as follows:
There are files in the subfolders of the dilapidated computers:
This section provides two methods.
Method 1 (modify the code from classmate Liu Yang)
To use this method, you must first set the vs2008 project attribute to multibyte. The displayed string will contain garbled characters.
Void findfile (char * filepath) {win32_find_data filedata; // these two are system parameters, hfile is the handle, filedata is the handle hfile, char dir [1000]; memset (Dir, 1000); sprintf_s (Dir, "% S % s", filepath ,"\\*. * "); hfile = findfirstfile (Dir, & filedata); If (hfile = invalid_handle_value) // findfirstfile () if this step is not found, {printf ("% s \ n", "file not found"); return;} bool bfinish = false; while (! Bfinish) {char * temp = filedata. cfilename; // This is the name of the object returned by findfirstfile () if (filedata. dwfileattributes & file_attribute_directory) & strcmp (temp ,". ") & strcmp (temp ,".. ") // determines whether the returned content is a directory or ". "".. ", because a directory contains ". ",".. "two folders: {char dirassist [3000]; memset (dirassist, 3000); sprintf_s (dirassist," % S % s ", filepath ,"\\", temp); findfile (dirassist);} // elseif (filedata. dwfileattributes & file_attribute_directory) = 0) // metadata is not a classlist.txt file {cout <filedata. cfilename <Endl;} bfinish = (findnextfile (hfile, & filedata) = false); // check whether there are any files in the directory. If yes, bfinish = flase continues to loop}
The result is as follows:
Method 2 (from Mao liuye, a netizen)
This version does not need to change the engineering character set. The default value is Unicode.
/*************************************** ********************************* // * Traverse folders */ /*************************************** * *******************************/void findfile (wchar_t * pfilepath) {win32_find_data findfiledata; handle hfind = bytes; wchar_t dirspec [max_path + 1]; // specify the path DWORD dwerror; wcsncpy (dirspec, pfilepath, wcslen (pfilepath) + 1 ); wcsncat (dirspec, l "\\\ *", 3); H Find = findfirstfile (dirspec, & findfiledata); If (hfind = invalid_handle_value) {wprintf (L "invalid file handle. error is % u ", getlasterror (); return;} else if (findfiledata. dwfileattributes! = File_attribute_directory) {wchar_t temp [3000]; memset (temp, * sizeof (wchar_t); wprintf_s (temp, l "% s \ n", pfilepath, findfiledata. cfilename); int ilen = widechartomultibyte (cp_acp, null, temp,-1, null, 0, null, false); // calculate the length of the converted string. (Including string Terminator) char * lpsz = new char [ilen]; widechartomultibyte (cp_oemcp, null, temp,-1, lpsz, ilen, null, false ); // formal conversion. Cout <lpsz <Endl; Delete [] lpsz;} else if (findfiledata. dwfileattributes = file_attribute_directory & wcscmp (findfiledata. cfilename, l ".")! = 0 & wcscmp (findfiledata. cfilename, l "..")! = 0) {// find the directory wchar_t dir [max_path + 1]; wcscpy (Dir, pfilepath); wcsncat (Dir, l "\", 2); wcscat (Dir, findfiledata. cfilename); findfile (DIR);} while (findnextfile (hfind, & findfiledata )! = 0) {If (findfiledata. dwfileattributes! = File_attribute_directory) {// find the file // fwprintf (FP, l "% s \", pfilepath); // fwprintf (FP, l "% s \ n ", findfiledata. cfilename); wchar_t temp [3000]; memset (temp, * sizeof (wchar_t); wcscpy (temp, pfilepath); wcscat (temp, findfiledata. cfilename); int ilen = widechartomultibyte (cp_acp, null, temp,-1, null, 0, null, false); // calculate the length of the converted string. (Including string Terminator) char * lpsz = new char [ilen]; widechartomultibyte (cp_oemcp, null, temp,-1, lpsz, ilen, null, false ); // formal conversion. Cout <lpsz <Endl; Delete [] lpsz;} else if (findfiledata. dwfileattributes = file_attribute_directory & wcscmp (findfiledata. cfilename, l ".")! = 0 & wcscmp (findfiledata. cfilename, l "..")! = 0) {// find the directory wchar_t dir [max_path + 1]; wcscpy (Dir, pfilepath); wcsncat (Dir, l "\", 2); wcscat (Dir, findfiledata. cfilename); findfile (DIR) ;}} dwerror = getlasterror (); findclose (hfind); If (dwerror! = Error_no_more_files) {wprintf (L "findnextfile error. error is % u", dwerror); Return ;}}
The running result is as follows: