路径暂时是写死的

编译两个版本的程序:

g++  treeT.cpp -municode -D_UNICODE -o treeT_UNI
g++  treeT.cpp -o treeT_ASC

为了观察ANSI版在遍历文件夹如果遇到Unicode字符会发生什么情况而写来作对比的

他们都可以接收终端传送的中文字符

ANSI版:

opendir/readdir 遍历目录遇到 UNICODE字符的时候会出问题

UNICODE版:

输出到stdout的时候,值>128 的UNICODE字符丢失

改为 WriteConsoleW 函数可以解决这个问题

 #include <iostream>
#include <cstdio>
#include <fcntl.h>
#include <sys/stat.h>
#include <dirent.h>
#include <tchar.h>
#include <cwchar>
#include <sys/types.h>
#include <cstring> #define NAME_MAX 1024 #ifdef _UNICODE
#define FMT_D "%ld"
#define FMT_S "%ls"
#define TXT_FILE "TREE_UTF.txt"
#else
#define FMT_D "%d"
#define FMT_S "%s"
#define TXT_FILE "TREE_ASC.txt"
#endif void func(TCHAR path[]); static FILE * fp = _tfopen( _TEXT( TXT_FILE ), _TEXT("wb")); int _tmain(int argc, TCHAR *argv[] )
{
TCHAR pth[] = _TEXT("D:\\Extra");
func(pth);
fclose(fp);
return ;
} void func(TCHAR path[])
{
_TDIR * a = _topendir(path);
_tdirent * dp;
_TDIR * aa;
struct _stat stbuf; TCHAR fullpath[NAME_MAX] = _TEXT(""); while (dp = _treaddir(a))
{
if (
_tcscmp(dp->d_name, _TEXT(".")) ==
|| _tcscmp(dp->d_name, _TEXT("..")) ==
)
{
continue;
} _stprintf(fullpath, _TEXT(FMT_S "\\" FMT_S), path, dp->d_name);
_tstat(fullpath, &stbuf); if ( (stbuf.st_mode & S_IFMT) == S_IFDIR )
{
func( fullpath );
}
else
{
//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 );
} }
_tclosedir(a);
}

可编译为 UNICODE 和 ANSI 版本的遍历目录树程序_0.1的更多相关文章

  1. 遍历目录树 - Unicode 模式

    =info     遍历目录树 支持 Unicode     Code by 523066680@163.com     2017-03         V0.5 使用Win32API判断目录硬链接 ...

  2. 有关UNICODE、ANSI字符集和相关字符串操作

    Q UNICODE字符串如何显示 A 如果程序定义了_UNICODE宏直接用 WCHAR *str=L"unicodestring"; TextOut(0,0,str); 否则就需 ...

  3. UNICODE与ANSI的区别

    什么是ANSI,什么又是UNICODE呢?其实这是两种不同的编码方式标准,ANSI中的字符采用8bit,而UNICODE中的字符采用16bit.(对于字符来说ANSI以单字节存放英文字符,以双字节存放 ...

  4. Unicode 和 ANSI

    Project Properties -> General-> Character set,里面显示了是不是unicode. Unicode处理String的方式不一样,一定要注意!!   ...

  5. [转]unicode,ansi,utf-8,unicode big endian的故事

    unicode,ansi,utf-8,unicode big endian的故事很久很久以前,有一群人,他们决定用8个可以开合的晶体管来组合成不同的状态,以表示世界上的万物.他们看到8个开关状态是好的 ...

  6. UNICODE和ANSI字符串的转换(解释了MultiByteToWideChar,WideCharToMultiByte,GetTextCharsetInfo,GetTextCharset,IsDBCSLeadByte,IsDBCSLeadByteEx,IsTextUnicode一共7个函数)

    继上集故事<多字符集(ANSI)和UNICODE及字符串处理方式准则 >,我们现在有一些特殊需求: 有时候我们的字符串是多字符型,我们却需要使用宽字符型:有的时候却恰恰相反. Window ...

  7. 字符编码(续)---Unicode与ANSI字符串转换以及分辨字符编码形式

    Unicode与ANSI字符串转换 我们使用windows函数MultiByteToWideChar将多字节字符串转换为宽字符字符串,如下: int MultiByteToWideChar( UINT ...

  8. cmake编译opencv时指定cuda版本

    之前有网友提问说,基于cmake编译时如果切换cuda版本,比如我同时装了cuda8和cuda9,opencv总是找到cuda9,我想用cuda8怎么办?实际上,手头上要配置的工程是基于opencv3 ...

  9. Creating Dialogbased Windows Application (4) / 创建基于对话框的Windows应用程序(四)Edit Control、Combo Box的应用、Unicode转ANSI、Open File Dialog、文件读取、可变参数、文本框自动滚动 / VC++, Windows

    创建基于对话框的Windows应用程序(四)—— Edit Control.Combo Box的应用.Unicode转ANSI.Open File Dialog.文件读取.可变参数.自动滚动 之前的介 ...

随机推荐

  1. OpenCV】透视变换 Perspective Transformation(续)

    载分 [OpenCV]透视变换 Perspective Transformation(续) 分类: [图像处理] [编程语言] 2014-05-27 09:39 2776人阅读 评论(13) 收藏 举 ...

  2. 编程语言 IDE 对比

    IDE是集成开发环境的英文缩写,所谓集成开发环境,就是将你在开发过程中所需要的工具或功能集成到了一起,比如代码编写.分析.编译.调试等功能,从而最大化地提高开发者的工作效率. 每种编程语言都有一些特定 ...

  3. 使用jQuery页面加载函数启动定时任务

    var timer; $(document).ready(function(){ if (timer == undefined) timer = window.setInterval(showTime ...

  4. Python: 测试函数是否被调用

    # helper class defined elsewhere class CallLogger(object): def __init__(self, meth): self.meth = met ...

  5. pod 出错备忘

    pod install #输出信息 /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems ...

  6. spring中bean配置和bean注入

    1 bean与spring容器的关系 Bean配置信息定义了Bean的实现及依赖关系,Spring容器根据各种形式的Bean配置信息在容器内部建立Bean定义注册表,然后根据注册表加载.实例化Bean ...

  7. U3D中IOS平台泛型方法尽少使用

    U3D的IOS最小运行库use micro mscorlib是不包含泛型反射方法的,如FieldType.GetGenericArguments方法.所以尽量少用List而直接使用array. 另外请 ...

  8. 命令行 更新Android sdk

    使用如下代理服务器: 大连东软信息学院镜像服务器地址: http://mirrors.neusoft.edu.cn 端口:80 北京化工大学镜像服务器地址: IPv4: http://ubuntu.b ...

  9. unity web player的debug和log信息

    win8模式下 unity web player的报错信息在如下目录下:C:\Users\xxx\AppData\Local\Temp\UnityWebPlayer\log 注:目录里的文件可能被隐藏 ...

  10. UIView的响应链

    父视图 和子视图的关联 只有当父视图  userInteractionEnabled=YES; 是其子视图才可响应 userInteractionEnabled=NO代表不接受响应 UIView的默认 ...