A traversal tree program that can be compiled into UNICODE and ANSI versions _0.1

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.