A word count program, completed by C + +, has the number of rows, the number of words, and the ability to complete the path of the file traversal.
The code to traverse the file section is as follows:
voidFindefile (wchar_t *Pfilepath) {CFileFind finder; CString Finddir; Finddir.format (Pfilepath); BOOL ret=Finder. FindFile (Finddir); while(ret) {ret=Finder. FindNextFile (); CString strpath=Finder. GetFilePath (); if(finder.) Isdirectory ()) {wchar_t*temp = Strpath.getbuffer (0); Temp+=strpath.getlength (); while(*temp! = ('\\') ) {temp--; } CString Filename=temp; if(Filename = ="\\."|| Filename = ="\\..") Continue;//If the directory is not processed Else{findefile (Strpath.getbuffer (0)); //TRACE ("Directory:%s\r\n", strpath); } } Else { //TRACE ("File:%s\r\n", strpath);wchar_t *test; Char*TEST; Test=Strpath.getbuffer (Strpath.getlength ()); TEST=WTOC (test); cout<<"\ n Files"<< TEST <<Endl; ReadFile (TEST); }} finder. Close (); }
View Code
is still the last problem, using some MFC functions, it is inevitable to use the wchar_t type, for this write two functions Wtoc and Ctow to achieve the two character type string conversion, the code is as follows:
Char* WTOC (wchar_t *a) { Char*TEST; size_t Len= Wcslen (a) +1; size_t converted=0; TEST= (Char*)malloc(len*sizeof(Char)); wcstombs_s (&converted, TEST, Len, A, _truncate); returnTEST;} wchar_t* Ctow (Char*a) {size_t len= Strlen (a) +1; size_t converted=0; wchar_t*test; Test= (wchar_t*)malloc(len*sizeof(wchar_t)); mbstowcs_s (&converted, test, Len, A, _truncate); returntest;}
View Code
At the beginning of the completion of the traversal of the file name can not be converted, more than can not be converted, even output is not output. In this part of the card for a long time, it is unknown why (after this problem has disappeared) in the future or as little as possible to use two types of character string mixed with the program.
Find the number of lines, word number part of the code is not affixed, write Tucson broken nothing to say.
Command-line arguments are used on the feature control, which is used for the first time. The main function is as follows:
intMainintargcChar*argv[]) { Char*filet ="D:\test\t.txt"; Wc wctest; if(argv[1][1] =='s') {Filet= argv[2]; wchar_t*F; F=Ctow (filet); Wctest.mod='o'; Wctest. HL=1; Wctest. Findefile (F); CIN>>filet; return 1; } if(ARGC = =1) {cout<<"Missing Parameters! "<<Endl; } Else if(ARGC = =2) {Wctest.mod='o'; Wctest. HL=1; Filet= argv[1]; } Else if(ARGC = =3) {Wctest.mod= argv[1][1]; Wctest. HL=0; Filet= argv[2]; } Else if(ARGC = =4) {Wctest.mod= argv[1][1]; if(argv[2][1] =='a') wctest. HL=1; ElseWctest. HL=0; Filet= argv[3]; } wctest. ReadFile (filet); CIN>>filet; return 0;}
View Code
Parameter-L is the number of rows checked,-W is the word count check,-C is the character number check,-A is the Advanced mode (blank line, Comment line, line of code),-O is the full output mode,-S is the traversal mode.
When the parameters are as follows
For the following folder:
The traversal results are as follows:
No further traversal of files under the sub-path.
Word count program, as well as the wide character and characters that plague people