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 ...
随机推荐
- .net 加水印 图片变大很多 解决方法
/// 给图片加水印 中国红木网 /// </summary> /// <param name="originalImg"> ...
- 《Differential Equations with Boundary-Value Problems》-chaper2-一阶线性方程
学习微分方程中,一个很常见的疑惑就是,我们所熟悉的非齐次微分方程的通解是对应齐次方程的通解加特解,但是更为重要的是,我们需要知道这句话是怎么得来的. 我们探讨一个未知问题的一般思路是将其不断的与已知已 ...
- dd usb 启动盘制作 成功版本
在linux系统中,使用dd命令制作启动盘成功.方法是在终端中输入命令: dd if=/root/opensuse.iso of=/dev/sdb bs=4M 说明: 1.o ...
- ABAP ALV表头的实现
ABAP实现ALV表头的DEMO: 效果: 源代码: *&------------------------------------------------------------------- ...
- 【转】国内较快的maven镜像
国内连接maven官方的仓库更新依赖库,网速一般很慢,收集一些国内快速的maven仓库镜像以备用. ====================国内OSChina提供的镜像,非常不错=========== ...
- Linux 内核开发—内核简单介绍
内核简单介绍 Linux 构成 Linux 为什么被划分为系统空间和内核空间 隔离核心程序和应用程序,实现对核心程序和数据的保护. 什么内核空间,用户空间 内核空间和用户空间是程序执行的两种不同的状态 ...
- getline和get的区别
#include<iostream> #include<fstream> #include<cstring> using namespace std; int ma ...
- SOA体系结构之基础培训教程-大纲篇
引言: 最近受邀做了一个企业的SOA体系结构的内训,本文是内训课程的培训大纲,分享一下吧,希望大家能够喜欢.同时也想针对大纲中列出的内容对SOA架构体系做一次回顾,如果时间允许把完整的课件也想放上来共 ...
- magic_quotes_sybase(魔术引号开关)
magic_quotes_sybase,如果该选项在php.ini文件中是唯一开启的话,将只会转义%00为\0(即null字符).此选项会完全覆盖magic_quotes_gpc.如果同时开启这两个选 ...
- JSP/Servlet 中的事件处理
写过AWT或Swing程序的人一定对桌面程序的事件处理机制印象深刻:通过实现Listener接口的类可以在特定事件(Event)发生时,呼叫特定的方法来对事件进行响应. 其实我们在编写JSP/Serv ...