window FILES——windows文件管理相关实例
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文件管理相关实例的更多相关文章
- 【转贴】Windows常用命令实例
Windows常用命令实例 https://www.cnblogs.com/linyfeng/p/6261629.html 熟练使用DOS常用命令有助于提高工作效率. 1.windows+R:打开运行 ...
- 重新想象 Windows 8 Store Apps (70) - 其它: 文件压缩和解压缩, 与 Windows 商店相关的操作, app 与 web, 几个 Core 的应用, 页面的生命周期和程序的生命周期
[源码下载] 重新想象 Windows 8 Store Apps (70) - 其它: 文件压缩和解压缩, 与 Windows 商店相关的操作, app 与 web, 几个 Core 的应用, 页面的 ...
- Eucalyptus-利用镜像启动一个Windows Server 2008r2实例
1.前言 使用kvm制作Eucalyptus镜像(Windows Server 2008r2为例)——http://www.cnblogs.com/gis-luq/p/3990792.html 上一篇 ...
- Linux文件管理相关命令
Linux文件管理相关命令 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 在了解了Linux文件管理背景知识之后, 我们可以 ...
- SQL[连载2]语法及相关实例
SQL[连载2]语法及相关实例 SQL语法 数据库表 一个数据库通常包含一个或多个表.每个表由一个名字标识(例如:"Websites"),表包含带有数据的记录(行). 在本教程中, ...
- Windows进程单实例运行
场景 Windows进程单实例运行,如果有进程没有退出,继续等待,直到进程完全退出,才会进入下一个实例 HANDLE pHandle = NULL; do { pHandle = ...
- LoadLibrary文件路径及windows API相关的文件路径问题
LoadLibrary HMODULE WINAPI LoadLibrary( _In_ LPCTSTR lpFileName ); Loads the specified module into ...
- 使用svnkit 的相关实例及相关问题汇总
SVNKIT操作SVN版本库的完整例子 http://www.cnblogs.com/wangjiyuan/p/svnkitwanchenglizi.html#!comments 2.SVNClien ...
- windows 系统相关配置
1. 外接显示器分辨率调节:连接上外接下显示器,在本机空白处,右键,分辨率.然后选择显示器,设置显示相关配置. 详见:http://zhidao.baidu.com/question/13494806 ...
随机推荐
- JavaScript高级程序设计27.pdf
第11章 DOM扩展 W3C将一些已经成为事实标准的专有扩展标准化并写入规范当中.对DOM的两个主要扩展是SelectorsAPI(选择符API)和HTML5 选择符API 就是根据CSS选择符选择与 ...
- Bzoj 1856: [Scoi2010]字符串 卡特兰数,乘法逆元,组合数,数论
1856: [Scoi2010]字符串 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1194 Solved: 651[Submit][Status][ ...
- hdoj 1166 敌兵布阵【线段树求区间最大值+单点更新】
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- JDk 内部分工具 简述
jar — 一个创建和管理 jar 文件的工具. java — Java 应用启动器.在这篇文章里,开发和部署都是用的这个启动器. javac — Java 编译器. javadoc — API 文档 ...
- DevExpress 用户控件 分页(上)
说明:使用用户控件分页,完成后,使用时非常简单,数据绑定,调用自己写的一个事件就OK了 前期准备工作: (1)添加一个用户控件 命名PageCtrl (2)打开代码: [csharp] view ...
- 使用PHPmailer发送邮件的详细代码
一.使用PHPMailer发送邮件的原因 PHP有内置的mail()方法,但是由于一些主机空间不支持该方法,所以经常会遇到无法发送邮件的情况. 所以,可以下载PHPMailer类,实现邮件发送. 二. ...
- ueditor编辑器图片自定义存放目录及路径修改
百度编辑器ueditor功能强大,很多人士以应用项目开发中,但是里面有一个公众的问题就是上传图片存放目录太深,默认是ueditor/php/upload下,前不久测试后图片存放目录可以改变,但是路径会 ...
- 在java项目中应用ueditor
虽然百度ueditor的官网和文档都已经很详细了.但是自己还是记录下 自己使用uEditor的过程. 这是 他的官网 http://ueditor.baidu.com/website/ 例子 文档什 ...
- [C++基础]关键词volatile
volatile概念 volatile 是易变的.不稳定的意思. volatile关键字和const 一样是一种类型修饰符,用它修饰的变量表示可以被某些编译器未知的因素更改,比如操作系统.硬件或者其它 ...
- linux kernel (proc文件系统)参数
http://blog.csdn.net/guowake/article/details/3279796 Linux Proc文件系统,通过对Proc文件系统进行调整,达到性能优化的目的. 二./pr ...