WinCE下读Ini文件函数
来源:互联网 发布:项韧带钙化 知乎 编辑:程序博客网 时间:2024/06/05 11:23
对于WinCE下读取Ini配置文件的时候,没有类似于XP桌面系统上的API函数 GetPrivateProfileString 等函数。要想对于INI配置文件的读写就要自己来实现这个函数。其实这个函数在WinCE系统开放出来的源码中已经有实现。自己已经稍稍的进行了改动,可以直接在工程中导入下来2个文件就使用这2个函数了。
在配置文件中 =( 等号) 两侧都不能有空格。
代码如下:
iniutil.h文件
//// Copyright (c) Microsoft Corporation. All rights reserved.////// Use of this sample source code is subject to the terms of the Microsoft// license agreement under which you licensed this sample source code. If// you did not accept the terms of the license agreement, you are not// authorized to use this sample source code. For the terms of the license,// please see the license agreement between you and Microsoft or, if applicable,// see the LICENSE.RTF on your install media or the root of your tools installation.// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.//#define lengthof(x) ( (sizeof((x))) / (sizeof(*(x))) )BOOL ReadIni(LPCWSTR pwszSection, LPCWSTR pwszKey, LPWSTR pwszValue, size_t cchValue, LPCWSTR pwszIniFile);BOOL WriteIni(LPCWSTR pwszSection, LPCWSTR pwszKey, LPWSTR pwszValue, size_t cchValue, LPCWSTR pwszIniFile);
iniutil.cpp文件
//// Copyright (c) Microsoft Corporation. All rights reserved.////// Use of this sample source code is subject to the terms of the Microsoft// license agreement under which you licensed this sample source code. If// you did not accept the terms of the license agreement, you are not// authorized to use this sample source code. For the terms of the license,// please see the license agreement between you and Microsoft or, if applicable,// see the LICENSE.RTF on your install media or the root of your tools installation.// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.//#include "stdafx.h"#include "iniutil.h"BOOL ReadIni(LPCWSTR pwszSection, LPCWSTR pwszKey, LPWSTR pwszValue, size_t cchValue, LPCWSTR pwszIniFile){ // GetPrivateProfileString is unavailable on CE BOOL bSuccess = FALSE; FILE * fin; WCHAR wszLine[256]; LPWSTR pwsz = NULL; if (!pwszIniFile || !pwszSection || !pwszKey || !pwszValue || !cchValue) { goto leave; } fin = _wfopen(pwszIniFile, L"r"); if (fin) { while (fgetws(wszLine, lengthof(wszLine), fin)) { if (!wcsstr(wszLine, pwszSection)) // Section { continue; } while (fgetws(wszLine, lengthof(wszLine), fin)) // Key & Value { pwsz = wcschr(wszLine, L'='); if (!pwsz) { continue; } *pwsz = L'\0'; // Erase the '=' if (0 != _wcsicmp(wszLine, pwszKey)) // Key { continue; } pwsz++; // Point to the value bSuccess = SUCCEEDED(StringCchCopyEx( pwszValue, cchValue, pwsz, &pwsz, NULL, 0 )); if (bSuccess) { if (*--pwsz == L'\n') { *pwsz = L'\0'; } } } } fclose(fin); }leave: return bSuccess;}BOOL WriteIni(LPCWSTR pwszSection, LPCWSTR pwszKey, LPWSTR pwszValue, size_t cchValue, LPCWSTR pwszIniFile){ // WritePrivateProfileString is unavailable on CE BOOL bSuccess = FALSE; BOOL bReplace; FILE * fin; fpos_t posSrc; fpos_t posDest; size_t cchLine; size_t cchNewLine; WCHAR wszLine[256]; WCHAR wszNewLine[256]; LPWSTR pwsz = NULL; DWORD dwAttrib; if (!pwszIniFile || !pwszSection || !pwszKey || (!pwszValue && cchValue) || (cchValue >= lengthof(wszLine))) { goto leave; } dwAttrib = GetFileAttributes(pwszIniFile); if (dwAttrib == -1) { goto leave; } SetFileAttributes(pwszIniFile, dwAttrib & ~FILE_ATTRIBUTE_READONLY); fin = _wfopen(pwszIniFile, L"r+"); if (fin) { bReplace = FALSE; cchNewLine = 0; while (fgetws(wszLine, lengthof(wszLine), fin)) { if (!wcsstr(wszLine, pwszSection)) // Section continue; if (fgetpos(fin, &posSrc) || fgetpos(fin, &posDest)) { goto closeFile; } while (fgetws(wszLine, lengthof(wszLine), fin)) // Key & Value { cchLine = wcslen(wszLine); pwsz = wcschr(wszLine, L'='); if (!pwsz) { goto nextLine; } *pwsz = L'\0'; // Erase the '=' if (0 != _wcsicmp(wszLine, pwszKey)) // Key { *pwsz = L'='; // Add the '=' back goto nextLine; } *pwsz = L'='; // Add the '=' back pwsz++; // Point to the value // Clear Key & Value on NULL or empty string if (!pwszValue || !cchValue) { bReplace = TRUE; cchLine = 0; goto nextLine; } // Do not overwritte more than the number of read characters. size_t cchKey = (pwsz - wszLine); if (SUCCEEDED(StringCchCopyNEx( wszNewLine, lengthof(wszNewLine), wszLine, cchKey, &pwsz, &cchNewLine, 0 )) && SUCCEEDED(StringCchCopyNEx( pwsz, cchNewLine, pwszValue, min((cchLine - cchKey), cchValue), &pwsz, &cchNewLine, STRSAFE_IGNORE_NULLS ))) { bReplace = TRUE; BOOL bAppendNewLine = FALSE; if (wszLine[cchLine-1] == L'\n') { cchLine--; bAppendNewLine = TRUE; } // See if there are remaining characters to copy if (cchValue > (cchLine - cchKey)) { if (bAppendNewLine) { cchLine++; } pwszValue += (cchLine - cchKey); if (FAILED(StringCchCopyNEx( wszLine, lengthof(wszLine), pwszValue, (cchValue - (cchLine - cchKey)), &pwsz, &cchLine, 0 )) || (bAppendNewLine && FAILED(StringCchCopy(pwsz, cchLine, L"\n"))) || FAILED(StringCchLength(wszLine, lengthof(wszLine), &cchLine))) { bReplace = FALSE; } } else { if (bAppendNewLine && FAILED(StringCchCopy(pwsz, cchNewLine, L"\n"))) { bReplace = FALSE; } cchLine = 0; } if (FAILED(StringCchLength(wszNewLine, lengthof(wszNewLine), &cchNewLine))) { bReplace = FALSE; } }nextLine: if (bReplace) { BOOL bEOF = (BOOL)feof(fin); // Save read offset, then restore write offset if (fgetpos(fin, &posSrc) || fsetpos(fin, &posDest)) { goto closeFile; } if (cchNewLine) { bSuccess = (WEOF != fputws(wszNewLine, fin)); if (cchLine && SUCCEEDED(StringCchCopyN( wszNewLine, lengthof(wszLine), wszLine, cchLine ))) { cchNewLine = cchLine; } else { cchNewLine = 0; } } else if (cchLine) { bSuccess = (WEOF != fputws(wszLine, fin)); cchLine = 0; } // Save write offset, then restore read offset if (fgetpos(fin, &posDest) || bEOF || fsetpos(fin, &posSrc)) { goto closeFile; } } else { // Just update read and write offsets if (fgetpos(fin, &posSrc) || fgetpos(fin, &posDest)) { goto closeFile; } } } }closeFile: if (bReplace && !fsetpos(fin, &posDest)) { if (cchNewLine) { bSuccess = (WEOF != fputws(wszNewLine, fin)); } SetEndOfFile(_fileno(fin)); } fclose(fin); } SetFileAttributes(pwszIniFile, dwAttrib);leave: return bSuccess;}
- WinCE下读Ini文件函数
- wince下ini文件操作
- Wince 下C#使用INI文件
- WinCE读取ini文件
- wince ini文件操作
- Wince读写INI文件
- WINCE 读取INI文件
- WinCE下配置(ini)文件读写,基于MFC的
- Wince 下C#使用INI文件(Bug修正版)
- Wince 下C#使用INI文件(Bug修正版)
- WINCE下读写INI配置文件
- WINCE C#读写INI文件
- WINCE C#读写INI文件
- wince下INI的操作更新
- wince 读写 ini 文件 操作 MFC
- 在 WinCe 平台读写 ini 文件
- 在 WinCe 平台读写 ini 文件
- 在 WinCe 平台读写 ini 文件
- Java加密技术(三) PBE
- 浏览器是如何工作的?(工作原理)
- 更新linux下python版本
- android 内存泄露调试
- linux input子系统的细节
- WinCE下读Ini文件函数
- hdu1372-Knight Moves--搜索
- R学习笔记之R特色与相关语法
- 程序员技术练级攻略
- Java加密技术(四)RSA
- C++四舍五入(2)
- jQuery不得不会---搭建jQuery开发环境
- Java加密技术(五)DH
- Windows 自旋锁分析(四)