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

  1 // TEST__WINDOWSFILES.cpp : Defines the entry point for the console application.
  2 //
  3
  4 #include "stdafx.h"
  5
  6 #include <iostream>
  7 #include <string>
  8
  9
 10 static wchar_t arrwReadBuffer[1024] = {0};
 11 static char arrReadBuffer[1024] = {0};
 12 static char arrReadBuffer_UTF8[1024] = {0};
 13
 14
 15 bool test__OperatorFile()
 16 {
 17     bool ret = false;
 18     HANDLE hFile = NULL;
 19     //wchar_t arrReadBuffer[1024] = {0};
 20
 21
 22     //test3.csv is UTF-16
 23     hFile = CreateFileW(TEXT("test3.csv"), GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
 24     if (INVALID_HANDLE_VALUE!=hFile)
 25     {
 26         if (TRUE==ReadFile(hFile, arrwReadBuffer, sizeof(arrwReadBuffer)-1, NULL, NULL))
 27         {
 28             printf("%s\n", arrwReadBuffer);
 29             std::wstring wstrReadBuffer(arrwReadBuffer);
 30             std::wcout << wstrReadBuffer << std::endl;
 31
 32             ret = true;
 33         }
 34     }
 35     else
 36     {
 37         ret = false;
 38     }
 39     CloseHandle(hFile);
 40     return ret;
 41 }
 42 bool test__OperatorFile2()
 43 {
 44     bool ret = false;
 45     HANDLE hFile = NULL;
 46     //char arrReadBuffer[1024] = {0};
 47
 48
 49     //test1.csv is GB2312
 50     hFile = CreateFileA(("test1.csv"), GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
 51     if (INVALID_HANDLE_VALUE!=hFile)
 52     {
 53         if (TRUE==ReadFile(hFile, arrReadBuffer, sizeof(arrReadBuffer)-1, NULL, NULL))
 54         {
 55             printf("%s\n", arrReadBuffer);
 56             std::string strReadBuffer(arrReadBuffer);
 57             std::cout << strReadBuffer << std::endl;
 58
 59             ret = true;
 60         }
 61     }
 62     else
 63     {
 64         ret = false;
 65     }
 66     CloseHandle(hFile);
 67     return ret;
 68 }
 69 bool test__OperatorFile3()
 70 {
 71     bool ret = false;
 72     HANDLE hFile = NULL;
 73     //wchar_t arrReadBuffer[1024] = {0};
 74
 75
 76     //test00.csv is UTF-16
 77     hFile = CreateFileW(TEXT("test00.csv"), GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
 78     if (INVALID_HANDLE_VALUE!=hFile)
 79     {
 80         if (TRUE==WriteFile(hFile, arrwReadBuffer, sizeof(arrwReadBuffer), NULL, NULL))
 81         {
 82
 83             ret = true;
 84         }
 85     }
 86     else
 87     {
 88         ret = false;
 89     }
 90     CloseHandle(hFile);
 91     return ret;
 92 }
 93 bool test__OperatorFile4()
 94 {
 95     bool ret = false;
 96     HANDLE hFile = NULL;
 97     //wchar_t arrReadBuffer[1024] = {0};
 98
 99
100     //test01.csv is GB2312
101     hFile = CreateFileA(("test01.csv"), GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
102     if (INVALID_HANDLE_VALUE!=hFile)
103     {
104         if (TRUE==WriteFile(hFile, arrReadBuffer, sizeof(arrReadBuffer), NULL, NULL))
105         {
106
107             ret = true;
108         }
109     }
110     else
111     {
112         ret = false;
113     }
114     CloseHandle(hFile);
115     return ret;
116 }
117 bool test__OperatorFile5()
118 {
119     bool ret = false;
120     HANDLE hFile = NULL;
121     //char arrReadBuffer[1024] = {0};
122
123
124     //test2.csv is UTF-8
125     hFile = CreateFileA(("test2.csv"), GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
126     if (INVALID_HANDLE_VALUE!=hFile)
127     {
128         if (TRUE==ReadFile(hFile, arrReadBuffer_UTF8, sizeof(arrReadBuffer_UTF8)-1, NULL, NULL))
129         {
130             printf("%s\n", arrReadBuffer_UTF8);
131             std::string strReadBuffer(arrReadBuffer_UTF8);
132             std::cout << strReadBuffer << std::endl;
133
134             ret = true;
135         }
136     }
137     else
138     {
139         ret = false;
140     }
141     CloseHandle(hFile);
142     return ret;
143 }
144 bool test__OperatorFile6()
145 {
146     bool ret = false;
147     HANDLE hFile = NULL;
148     //wchar_t arrReadBuffer[1024] = {0};
149
150
151     //test02.csv is UTF-8
152     hFile = CreateFileA(("test02.csv"), GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
153     if (INVALID_HANDLE_VALUE!=hFile)
154     {
155         if (TRUE==WriteFile(hFile, arrReadBuffer_UTF8, sizeof(arrReadBuffer_UTF8), NULL, NULL))
156         {
157
158             ret = true;
159         }
160     }
161     else
162     {
163         ret = false;
164     }
165     CloseHandle(hFile);
166     return ret;
167 }
168
169 CHAR *lpReadBuffer_UTF8 = NULL;
170 DWORD cbReadBuffer_UTF8 = 0;
171 WCHAR *lpReadBuffer_UTF16 = NULL;
172 INT cchReadBuffer_UTF16 = 0;
173 std::wstring wstrReadBuffer;
174 bool test__OperatorFile7()
175 {
176     bool ret = false;
177     HANDLE hFile = NULL;
178     //char arrReadBuffer[1024] = {0};
179
180
181     //test2.csv is UTF-8
182     hFile = CreateFileA(("test2.csv"), GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
183     if (INVALID_HANDLE_VALUE!=hFile)
184     {
185         cbReadBuffer_UTF8 = SetFilePointer(hFile, 0, NULL, FILE_END);
186         if (sizeof(CHAR)!=sizeof(BYTE))
187         {
188             return ret;
189         }
190         lpReadBuffer_UTF8 = (CHAR *)malloc( cbReadBuffer_UTF8 );
191         ZeroMemory(lpReadBuffer_UTF8, cbReadBuffer_UTF8);
192         SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
193
194         if (NULL!=lpReadBuffer_UTF8 && TRUE==ReadFile(hFile, lpReadBuffer_UTF8, cbReadBuffer_UTF8-1, NULL, NULL))
195         {
196             lpReadBuffer_UTF16 = (WCHAR *)malloc( cbReadBuffer_UTF8*sizeof(WCHAR) );
197             ZeroMemory(lpReadBuffer_UTF16, cbReadBuffer_UTF8*sizeof(WCHAR));
198             cchReadBuffer_UTF16 = cbReadBuffer_UTF8;
199             if ( 0!=MultiByteToWideChar(CP_UTF8, 0, lpReadBuffer_UTF8, cbReadBuffer_UTF8, lpReadBuffer_UTF16, cchReadBuffer_UTF16) )
200             {
201                 wstrReadBuffer = lpReadBuffer_UTF16;
202                 std::wcout << wstrReadBuffer << std::endl;
203
204                 WCHAR *lpReadBuffer_UTF16_2 = (WCHAR *)malloc( cbReadBuffer_UTF8*sizeof(WCHAR) );
205                 ZeroMemory(lpReadBuffer_UTF16_2, cbReadBuffer_UTF8*sizeof(WCHAR));
206                 CopyMemory(lpReadBuffer_UTF16_2, wstrReadBuffer.c_str(), (wstrReadBuffer.length()+1)*sizeof(WCHAR));
207                 if (lpReadBuffer_UTF16_2)
208                     free(lpReadBuffer_UTF16_2);
209
210                 WCHAR *lpReadBuffer_UTF16_3 = (WCHAR *)malloc( cbReadBuffer_UTF8*sizeof(WCHAR) );
211                 ZeroMemory(lpReadBuffer_UTF16_3, cbReadBuffer_UTF8*sizeof(WCHAR));
212                 lstrcpyn(lpReadBuffer_UTF16_3, wstrReadBuffer.c_str(), (wstrReadBuffer.length()+1));
213                 if (lpReadBuffer_UTF16_3)
214                     free(lpReadBuffer_UTF16_3);
215
216                 ret = true;
217
218             }
219
220
221
222             if (lpReadBuffer_UTF16)
223                 free(lpReadBuffer_UTF16);
224         }
225
226         if (lpReadBuffer_UTF8)
227             free(lpReadBuffer_UTF8);
228
229
230     }
231     else
232     {
233         ret = false;
234     }
235     CloseHandle(hFile);
236     return ret;
237 }
238
239 WCHAR *lpWriteBuffer_UTF16 = NULL;
240 INT cchWriteBuffer_UTF16 = 0;
241 CHAR *lpWriteBuffer_UTF8 = NULL;
242 INT cbWriteBuffer_UTF8 = 0;
243 bool test__OperatorFile8()
244 {
245     bool ret = false;
246     HANDLE hFile = NULL;
247     //wchar_t arrReadBuffer[1024] = {0};
248
249
250     //test02.csv is UTF-8
251     hFile = CreateFileA(("test03.csv"), GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
252     if (INVALID_HANDLE_VALUE!=hFile)
253     {
254         cchWriteBuffer_UTF16 = wstrReadBuffer.length()+1;
255         lpWriteBuffer_UTF16 = (WCHAR *)malloc( cchWriteBuffer_UTF16*sizeof(WCHAR) );
256         ZeroMemory(lpWriteBuffer_UTF16, cchWriteBuffer_UTF16*sizeof(WCHAR));
257         CopyMemory(lpWriteBuffer_UTF16, wstrReadBuffer.c_str(), cchWriteBuffer_UTF16*sizeof(WCHAR));
258
259         if (sizeof(CHAR)!=sizeof(BYTE))
260         {
261             return ret;
262         }
263         cbWriteBuffer_UTF8 = cchWriteBuffer_UTF16*sizeof(WCHAR);
264         lpWriteBuffer_UTF8 = (CHAR *)malloc(cbWriteBuffer_UTF8);
265         ZeroMemory(lpWriteBuffer_UTF8, cbWriteBuffer_UTF8);
266
267         if (0!=WideCharToMultiByte(CP_UTF8, 0, lpWriteBuffer_UTF16, cchWriteBuffer_UTF16, lpWriteBuffer_UTF8, cbWriteBuffer_UTF8, NULL, NULL))
268         {
269             SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
270             if (TRUE==WriteFile(hFile, lpWriteBuffer_UTF8, cbWriteBuffer_UTF8, NULL, NULL))
271             {
272
273                 ret = true;
274             }
275
276         }
277
278         if (lpWriteBuffer_UTF8)
279             free(lpWriteBuffer_UTF8);
280
281         if (lpWriteBuffer_UTF16)
282             free(lpWriteBuffer_UTF16);
283     }
284     else
285     {
286         ret = false;
287     }
288     CloseHandle(hFile);
289     return ret;
290 }
291
292
293 int _tmain(int argc, _TCHAR* argv[])
294 {
295     //std::cout << std::boolalpha << test__OperatorFile() << std::endl;
296     //std::cout << std::boolalpha << test__OperatorFile2() << std::endl;
297     //std::cout << std::boolalpha << test__OperatorFile3() << std::endl;
298     //std::cout << std::boolalpha << test__OperatorFile4() << std::endl;
299     //std::cout << std::boolalpha << test__OperatorFile5() << std::endl;
300     //std::cout << std::boolalpha << test__OperatorFile6() << std::endl;
301     std::cout << std::boolalpha << test__OperatorFile7() << std::endl;
302     std::cout << std::boolalpha << test__OperatorFile8() << std::endl;
303
304     return 0;
305 }

*注:编译环境VS2012。

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

时间: 2024-10-05 04:44:52

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

【转贴】Windows常用命令实例

Windows常用命令实例 https://www.cnblogs.com/linyfeng/p/6261629.html 熟练使用DOS常用命令有助于提高工作效率. 1.windows+R:打开运行程序窗口 2.cmd:调用DOS命令窗口 3.mstsc:调用远程桌面命令窗口.mstsc -v 192.168..0.1  远程访问192.168.0.1服务器.如下 4.mspaint:调用画板 5.regedit:调用注册表 6.services.msc:调用系统服务 7.dir:显示当前目录

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

SVNKIT操作SVN版本库的完整例子 http://www.cnblogs.com/wangjiyuan/p/svnkitwanchenglizi.html#!comments 2.SVNClientManager包括了各种Client来完成日常的SVN操作,如下图: 类 命令 备注 SVNLogClient.doLog() svn log [PATHURL] 从库中显示log消息  SVNLogClient.doList() svn list [TARGET[@REV]...] 显示目标下的

Linux文件管理相关命令

作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 在了解了Linux文件管理背景知识之后, 我们可以学习一些命令来管理我们的文件. 文件操作相关 有一些命令可以帮助我们"修剪"之前看到的文件树. $touch a.txt 如果a.txt不存在,生成一个新的空文档a.txt.如果a.txt存在,那么只更改该文档的时间信息.(这个命令实际上用得并不广泛,但可以帮我们创建一个空文件来实验下面操作) $ls . 是list的简

Linux学习记录--文件管理相关系统编程

文件管理相关系统编程 重要文件标识 打开文件标识 O_RDONLY:只读方式打开 O_WRONLY:只写方式打开 O_RDWR:可读写方式打开 打开文件操作副标识 O_CREAT:若路径中文件不存在则创建,使用Open函数时需同时指定文件权限 O_EXCL:若与O_CREAT连用,检查文件是否已经存在,若不存在则建立文件存在则返回错误,这使创建和测试成为一个原子操作 O_APPEND:读写文件从文件尾部开始移动,所有写入数据都加入文件尾部 O_TRUNC:若文件存在并且可以写入,此标识会将源文件

Windows服务简单实例

1.定时器使用 partial class TimerService : ServiceBase { public TimerService() { InitializeComponent(); } Timer timer = new Timer(); protected override void OnStart(string[] args) { // TODO: 在此处添加代码以启动服务. AddLog("****服务启动"); timer.Elapsed += timer_Ela

linux文件管理相关的命令

linux文件管理相关的命令 文件元数据的查看(文件自身的属性) stat: 获取指定文件的元数据 $ stat test File: `test' Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 802h/2050d Inode: 651942 Links: 1 Access: (0664/-rw-rw-r--) Uid: ( 500/ centos) Gid: ( 500/ centos) Access: 2018-08

一文带你了解建造者模式及相关实例解析

1.简介 The builder pattern is a design pattern designed to provide a flexible solution to various object creation problems in object-oriented programming. The intent of the Builder design pattern is to separate the construction of a complex object from

C# windows 服务 操作实例

先说明一下windows服务开发前要明白的东西,DOS 程序所在物理路径D:\workObject\xx.exe 输入CMDcd C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727cd C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319 安装服务installUtil D:\workObject\xx.exe 卸载服务installUtil /u D:\workObject\xx.exe 查看Window服务serv

Windows Unicode相关

====头文件前后顺序影响TCHAR定义===============================windows.h winnt.htchar.h======定义_UNICODE宏常量控制==================//字符typedef char TCHAR;typedef wchar_t TCHAR; 字符串typedef __T(x) L##xtypedef __T(x) x //C库函数#define _tcslen strlen#define _tcslen wcslen#