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。
具体源码我已打包上传:https://files.cnblogs.com/superstargg/TEST__WINDOWSFILES.zip
煮酒论英雄