The path was temporarily written dead.
Compile two versions of the program:
g++ Treet.cpp-municode-d_unicode-o Treet_uni
g++ Treet.cpp-o TREET_ASC
To see what happens to the ANSI version when traversing a folder if it encounters Unicode characters, write a comparison
They can all receive the Chinese characters of the terminal transfer
ANSI version:
Opendir/readdir traversing a directory when encountering Unicode characters can cause problems
Unicode version:
When output to stdout, the value >128 Unicode character is missing
Change to Writeconsolew function to solve this problem
1#include <iostream>2#include <cstdio>3#include <fcntl.h>4#include <sys/stat.h>5#include <dirent.h>6#include <tchar.h>7#include <cwchar>8#include <sys/types.h>9#include <cstring>Ten One #defineName_max 1024 A - #ifdef _unicode - #defineFmt_d "%ld" the #definefmt_s "%ls" - #defineTxt_file "Tree_utf.txt" - #else - #defineFmt_d "%d" + #definefmt_s "%s" - #defineTxt_file "Tree_asc.txt" + #endif A at voidfunc (TCHAR path[]); - - StaticFILE * fp = _tfopen (_text (Txt_file), _text ("WB")); - - int_tmain (intARGC, TCHAR *argv[]) - { inTCHAR pth[] = _text ("D:\\extra"); - func (PTH); to fclose (FP); + return 0; - } the * voidfunc (TCHAR path[]) $ {Panax Notoginseng_tdir * a =_topendir (path); -_tdirent *DP; the_tdir *AA; + struct_stat stbuf; A theTCHAR Fullpath[name_max] = _text (""); + - while(DP =_treaddir (a)) $ { $ if ( -_TCSCMP (Dp->d_name, _text (".")) ==0 -|| _TCSCMP (Dp->d_name, _text ("..")) ==0 the ) - {Wuyi Continue; the } - Wu_stprintf (FullPath, _text (fmt_s"\\"fmt_s), Path, dp->d_name); -_tstat (FullPath, &stbuf); About $ if((Stbuf.st_mode & s_ifmt) = =S_ifdir) - { - func (fullpath); - } A Else + { the //Output File list -_ftprintf (FP, _text (fmt_d"\ t"fmt_s"\ r \ n"), Stbuf.st_mtime, fullpath); $_ftprintf (stdout, _text (fmt_d"\ t"fmt_s"\ r \ n"), Stbuf.st_mtime, fullpath); the } the the } the _tclosedir (a); - } in the
A traversal tree program that can be compiled into UNICODE and ANSI versions _0.1