C语言下有一套文件管理方案、C++语言下也有一套自己的文件管理方案、windows系统当然也有自己的一套文件管理方案啦。对于普通char类型为基础的字符使用哪种方案的解决办法都是一样的,但是对于宽字符wchar_t还是用windows自带的解决方案比较好。尤其对于中来说。

下面是windows文件读写编码转换相关的实例。

msdn上可以参考: http://msdn.microsoft.com/en-us/library/windows/desktop/aa364233%28v=vs.85%29.aspx

 // TEST__WINDOWSFILES.cpp : Defines the entry point for the console application.
// #include "stdafx.h" #include <iostream>
#include <string> static wchar_t arrwReadBuffer[] = {};
static char arrReadBuffer[] = {};
static char arrReadBuffer_UTF8[] = {}; bool test__OperatorFile()
{
bool ret = false;
HANDLE hFile = NULL;
//wchar_t arrReadBuffer[1024] = {0}; //test3.csv is UTF-16
hFile = CreateFileW(TEXT("test3.csv"), GENERIC_READ|GENERIC_WRITE, , NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE!=hFile)
{
if (TRUE==ReadFile(hFile, arrwReadBuffer, sizeof(arrwReadBuffer)-, NULL, NULL))
{
printf("%s\n", arrwReadBuffer);
std::wstring wstrReadBuffer(arrwReadBuffer);
std::wcout << wstrReadBuffer << std::endl; ret = true;
}
}
else
{
ret = false;
}
CloseHandle(hFile);
return ret;
}
bool test__OperatorFile2()
{
bool ret = false;
HANDLE hFile = NULL;
//char arrReadBuffer[1024] = {0}; //test1.csv is GB2312
hFile = CreateFileA(("test1.csv"), GENERIC_READ|GENERIC_WRITE, , NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE!=hFile)
{
if (TRUE==ReadFile(hFile, arrReadBuffer, sizeof(arrReadBuffer)-, NULL, NULL))
{
printf("%s\n", arrReadBuffer);
std::string strReadBuffer(arrReadBuffer);
std::cout << strReadBuffer << std::endl; ret = true;
}
}
else
{
ret = false;
}
CloseHandle(hFile);
return ret;
}
bool test__OperatorFile3()
{
bool ret = false;
HANDLE hFile = NULL;
//wchar_t arrReadBuffer[1024] = {0}; //test00.csv is UTF-16
hFile = CreateFileW(TEXT("test00.csv"), GENERIC_READ|GENERIC_WRITE, , NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE!=hFile)
{
if (TRUE==WriteFile(hFile, arrwReadBuffer, sizeof(arrwReadBuffer), NULL, NULL))
{ ret = true;
}
}
else
{
ret = false;
}
CloseHandle(hFile);
return ret;
}
bool test__OperatorFile4()
{
bool ret = false;
HANDLE hFile = NULL;
//wchar_t arrReadBuffer[1024] = {0}; //test01.csv is GB2312
hFile = CreateFileA(("test01.csv"), GENERIC_READ|GENERIC_WRITE, , NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE!=hFile)
{
if (TRUE==WriteFile(hFile, arrReadBuffer, sizeof(arrReadBuffer), NULL, NULL))
{ ret = true;
}
}
else
{
ret = false;
}
CloseHandle(hFile);
return ret;
}
bool test__OperatorFile5()
{
bool ret = false;
HANDLE hFile = NULL;
//char arrReadBuffer[1024] = {0}; //test2.csv is UTF-8
hFile = CreateFileA(("test2.csv"), GENERIC_READ|GENERIC_WRITE, , NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE!=hFile)
{
if (TRUE==ReadFile(hFile, arrReadBuffer_UTF8, sizeof(arrReadBuffer_UTF8)-, NULL, NULL))
{
printf("%s\n", arrReadBuffer_UTF8);
std::string strReadBuffer(arrReadBuffer_UTF8);
std::cout << strReadBuffer << std::endl; ret = true;
}
}
else
{
ret = false;
}
CloseHandle(hFile);
return ret;
}
bool test__OperatorFile6()
{
bool ret = false;
HANDLE hFile = NULL;
//wchar_t arrReadBuffer[1024] = {0}; //test02.csv is UTF-8
hFile = CreateFileA(("test02.csv"), GENERIC_READ|GENERIC_WRITE, , NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE!=hFile)
{
if (TRUE==WriteFile(hFile, arrReadBuffer_UTF8, sizeof(arrReadBuffer_UTF8), NULL, NULL))
{ ret = true;
}
}
else
{
ret = false;
}
CloseHandle(hFile);
return ret;
} CHAR *lpReadBuffer_UTF8 = NULL;
DWORD cbReadBuffer_UTF8 = ;
WCHAR *lpReadBuffer_UTF16 = NULL;
INT cchReadBuffer_UTF16 = ;
std::wstring wstrReadBuffer;
bool test__OperatorFile7()
{
bool ret = false;
HANDLE hFile = NULL;
//char arrReadBuffer[1024] = {0}; //test2.csv is UTF-8
hFile = CreateFileA(("test2.csv"), GENERIC_READ|GENERIC_WRITE, , NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE!=hFile)
{
cbReadBuffer_UTF8 = SetFilePointer(hFile, , NULL, FILE_END);
if (sizeof(CHAR)!=sizeof(BYTE))
{
return ret;
}
lpReadBuffer_UTF8 = (CHAR *)malloc( cbReadBuffer_UTF8 );
ZeroMemory(lpReadBuffer_UTF8, cbReadBuffer_UTF8);
SetFilePointer(hFile, , NULL, FILE_BEGIN); if (NULL!=lpReadBuffer_UTF8 && TRUE==ReadFile(hFile, lpReadBuffer_UTF8, cbReadBuffer_UTF8-, NULL, NULL))
{
lpReadBuffer_UTF16 = (WCHAR *)malloc( cbReadBuffer_UTF8*sizeof(WCHAR) );
ZeroMemory(lpReadBuffer_UTF16, cbReadBuffer_UTF8*sizeof(WCHAR));
cchReadBuffer_UTF16 = cbReadBuffer_UTF8;
if ( !=MultiByteToWideChar(CP_UTF8, , lpReadBuffer_UTF8, cbReadBuffer_UTF8, lpReadBuffer_UTF16, cchReadBuffer_UTF16) )
{
wstrReadBuffer = lpReadBuffer_UTF16;
std::wcout << wstrReadBuffer << std::endl; WCHAR *lpReadBuffer_UTF16_2 = (WCHAR *)malloc( cbReadBuffer_UTF8*sizeof(WCHAR) );
ZeroMemory(lpReadBuffer_UTF16_2, cbReadBuffer_UTF8*sizeof(WCHAR));
CopyMemory(lpReadBuffer_UTF16_2, wstrReadBuffer.c_str(), (wstrReadBuffer.length()+)*sizeof(WCHAR));
if (lpReadBuffer_UTF16_2)
free(lpReadBuffer_UTF16_2); WCHAR *lpReadBuffer_UTF16_3 = (WCHAR *)malloc( cbReadBuffer_UTF8*sizeof(WCHAR) );
ZeroMemory(lpReadBuffer_UTF16_3, cbReadBuffer_UTF8*sizeof(WCHAR));
lstrcpyn(lpReadBuffer_UTF16_3, wstrReadBuffer.c_str(), (wstrReadBuffer.length()+));
if (lpReadBuffer_UTF16_3)
free(lpReadBuffer_UTF16_3); ret = true; } if (lpReadBuffer_UTF16)
free(lpReadBuffer_UTF16);
} if (lpReadBuffer_UTF8)
free(lpReadBuffer_UTF8); }
else
{
ret = false;
}
CloseHandle(hFile);
return ret;
} WCHAR *lpWriteBuffer_UTF16 = NULL;
INT cchWriteBuffer_UTF16 = ;
CHAR *lpWriteBuffer_UTF8 = NULL;
INT cbWriteBuffer_UTF8 = ;
bool test__OperatorFile8()
{
bool ret = false;
HANDLE hFile = NULL;
//wchar_t arrReadBuffer[1024] = {0}; //test02.csv is UTF-8
hFile = CreateFileA(("test03.csv"), GENERIC_READ|GENERIC_WRITE, , NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE!=hFile)
{
cchWriteBuffer_UTF16 = wstrReadBuffer.length()+;
lpWriteBuffer_UTF16 = (WCHAR *)malloc( cchWriteBuffer_UTF16*sizeof(WCHAR) );
ZeroMemory(lpWriteBuffer_UTF16, cchWriteBuffer_UTF16*sizeof(WCHAR));
CopyMemory(lpWriteBuffer_UTF16, wstrReadBuffer.c_str(), cchWriteBuffer_UTF16*sizeof(WCHAR)); if (sizeof(CHAR)!=sizeof(BYTE))
{
return ret;
}
cbWriteBuffer_UTF8 = cchWriteBuffer_UTF16*sizeof(WCHAR);
lpWriteBuffer_UTF8 = (CHAR *)malloc(cbWriteBuffer_UTF8);
ZeroMemory(lpWriteBuffer_UTF8, cbWriteBuffer_UTF8); if (!=WideCharToMultiByte(CP_UTF8, , lpWriteBuffer_UTF16, cchWriteBuffer_UTF16, lpWriteBuffer_UTF8, cbWriteBuffer_UTF8, NULL, NULL))
{
SetFilePointer(hFile, , NULL, FILE_BEGIN);
if (TRUE==WriteFile(hFile, lpWriteBuffer_UTF8, cbWriteBuffer_UTF8, NULL, NULL))
{ ret = true;
} } if (lpWriteBuffer_UTF8)
free(lpWriteBuffer_UTF8); if (lpWriteBuffer_UTF16)
free(lpWriteBuffer_UTF16);
}
else
{
ret = false;
}
CloseHandle(hFile);
return ret;
} int _tmain(int argc, _TCHAR* argv[])
{
//std::cout << std::boolalpha << test__OperatorFile() << std::endl;
//std::cout << std::boolalpha << test__OperatorFile2() << std::endl;
//std::cout << std::boolalpha << test__OperatorFile3() << std::endl;
//std::cout << std::boolalpha << test__OperatorFile4() << std::endl;
//std::cout << std::boolalpha << test__OperatorFile5() << std::endl;
//std::cout << std::boolalpha << test__OperatorFile6() << std::endl;
std::cout << std::boolalpha << test__OperatorFile7() << std::endl;
std::cout << std::boolalpha << test__OperatorFile8() << std::endl; return ;
}

*注:编译环境VS2012。

具体源码我已打包上传:http://files.cnblogs.com/superstargg/TEST__WINDOWSFILES.zip

window FILES——windows文件管理相关实例的更多相关文章

  1. 【转贴】Windows常用命令实例

    Windows常用命令实例 https://www.cnblogs.com/linyfeng/p/6261629.html 熟练使用DOS常用命令有助于提高工作效率. 1.windows+R:打开运行 ...

  2. 重新想象 Windows 8 Store Apps (70) - 其它: 文件压缩和解压缩, 与 Windows 商店相关的操作, app 与 web, 几个 Core 的应用, 页面的生命周期和程序的生命周期

    [源码下载] 重新想象 Windows 8 Store Apps (70) - 其它: 文件压缩和解压缩, 与 Windows 商店相关的操作, app 与 web, 几个 Core 的应用, 页面的 ...

  3. Eucalyptus-利用镜像启动一个Windows Server 2008r2实例

    1.前言 使用kvm制作Eucalyptus镜像(Windows Server 2008r2为例)——http://www.cnblogs.com/gis-luq/p/3990792.html 上一篇 ...

  4. Linux文件管理相关命令

    Linux文件管理相关命令   作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 在了解了Linux文件管理背景知识之后, 我们可以 ...

  5. SQL[连载2]语法及相关实例

    SQL[连载2]语法及相关实例 SQL语法 数据库表 一个数据库通常包含一个或多个表.每个表由一个名字标识(例如:"Websites"),表包含带有数据的记录(行). 在本教程中, ...

  6. Windows进程单实例运行

    场景         Windows进程单实例运行,如果有进程没有退出,继续等待,直到进程完全退出,才会进入下一个实例 HANDLE pHandle = NULL; do  {  pHandle = ...

  7. LoadLibrary文件路径及windows API相关的文件路径问题

    LoadLibrary HMODULE WINAPI LoadLibrary( _In_  LPCTSTR lpFileName ); Loads the specified module into ...

  8. 使用svnkit 的相关实例及相关问题汇总

    SVNKIT操作SVN版本库的完整例子 http://www.cnblogs.com/wangjiyuan/p/svnkitwanchenglizi.html#!comments 2.SVNClien ...

  9. windows 系统相关配置

    1. 外接显示器分辨率调节:连接上外接下显示器,在本机空白处,右键,分辨率.然后选择显示器,设置显示相关配置. 详见:http://zhidao.baidu.com/question/13494806 ...

随机推荐

  1. UVA 3890 Most Distant Point from the Sea(二分法+半平面交)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=11358 [思路] 二分法+半平面交 二分与海边的的距离,由法向量可 ...

  2. Eclipse编译ijkplayer

    参考链接:http://blog.csdn.net/fatiao101/article/details/49586379

  3. 【转】linux下安装opencv

    Installation in Linux These steps have been tested for Ubuntu 10.04 but should work with other distr ...

  4. In Java, what is the default location for newly created files?

    If the current directory of the application. If e.g. you create a File by using new FileOutputStream ...

  5. PHP开发APP接口(一)

    php以json或者xml 形式返回给app.明白这点就很好说了,就是把数据包装成json或者xml,返回给APP 定义抽象APP基类: <?php /** * 定义API抽象类 */ abst ...

  6. 使用C++的开源序列化(Serialization)库cereal

    博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:使用C++的开源序列化(Serialization)库cereal.

  7. springboot +spring security4 自定义手机号码+短信验证码登录

    spring security 默认登录方式都是用户名+密码登录,项目中使用手机+ 短信验证码登录, 没办法,只能实现修改: 需要修改的地方: 1 .自定义 AuthenticationProvide ...

  8. MySQL 使用while语句向数据表中批量插入数据

    1.创建一张数据表 mysql> create table test_while ( -> id int primary key) charset = utf8; Query OK, ro ...

  9. Android怎样改动app不在多任务列表中显示

    在实际开发中,我们希望某些activity或者应用程序不在多任务列表中显示,即长按Home键或者多任务button键不显示近期执行的程序,我们能够在对应应用程序的AndroidManifest.xml ...

  10. 分享2D Unity游戏的动画制作经验

    作者:Alex Rose Unity近期宣布推出额外的2D游戏支持,加入了Box 2D物理和一个精灵管理器. 但这里还是有些技巧须要牢记在心.逐帧更改图像仅仅是动画制作的冰山一角,若要让你的游戏出色执 ...