ini文件读入思考 GetPrivateProfileString( )

yunsongblue 2018-06-07 08:05:47
有如下形式的ini文件,如何编写才能读出相应的键名所对应的值?

[b][节1]
键名集合的名称=键名1|键名2|键名3
键所对应值集合的名称=键名1的值|键名2的值|键名3的值
[/b]

GetPrivateProfileString( )
搜索的格式是
[节1]
键名1=键名1的值
怎么编写才能变成这种,当读键名1时取键名1的值,当读键名2时取键名2的值,当读键名3时取键名3的值
[节1]
键名集合的名称=键名1|键名2|键名3
键所对应值集合的名称=键名1的值|键名2的值|键名3的值
...全文
802 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
待续_1006 2018-06-07
  • 打赏
  • 举报
回复
[MAC_ADDRESS] 11=53B1CD855E070C26734B1E2EA46870D1B1578287782CE801F99685D05FD76103 12=53B1CD855E070C26734B1E2EA46870D1B1578287782CE801F99685D05FD76103 13=53B1CD855E070C26734B1E2EA46870D1B1578287782CE801F99685D05FD76103 14=53B1CD855E070C26734B1E2EA46870D1B1578287782CE801F99685D05FD76103 这不是随你心情的事吗
赵4老师 2018-06-07
  • 打赏
  • 举报
回复
//文件c:\tmp\tmp\test.ini的内容:
//[节1]
//键名集合的名称=键名1|键名2|键名3
//键所对应值集合的名称=键名1的值|键名2的值|键名3的值
//#define _UNICODE
//#define UNICODE
#pragma comment(lib,"advapi32")
#include <tchar.h>
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <locale.h>
int n;
TCHAR ks[256],vs[256];
TCHAR *kts[100],*vts[100];
TCHAR *V(TCHAR *K) {
    int i;
    for (i=0;i<n;i++) {
        if (0==_tcscmp(kts[i],K)) {
            return vts[i];
        }
    }
    return _T("");
}
int main() {
    int i;
    TCHAR *kt,*vt;

    setlocale(LC_ALL,"chs");

    GetPrivateProfileString(_T("节1"),_T("键名集合的名称"      ),_T(""),ks,256,_T("c:\\tmp\\tmp\\test.ini"));
    _tprintf(_T("ks[%s]\n"),ks);
    kt=_tcstok(ks,_T("|"));
    i=0;
    while (kt!=NULL) {
        kts[i]=kt;
        i++;if (i>=100) break;
        kt=_tcstok(NULL,_T("|"));
    }
    GetPrivateProfileString(_T("节1"),_T("键所对应值集合的名称"),_T(""),vs,256,_T("c:\\tmp\\tmp\\test.ini"));
    _tprintf(_T("vs[%s]\n"),vs);
    vt=_tcstok(vs,_T("|"));
    i=0;
    while (vt!=NULL) {
        vts[i]=vt;
        i++;if (i>=100) break;
        vt=_tcstok(NULL,_T("|"));
    }
    n=i;
    _tprintf(_T("[%s]:%s\n"),_T("键名1"),V(_T("键名1")));
    _tprintf(_T("[%s]:%s\n"),_T("键名2"),V(_T("键名2")));
    _tprintf(_T("[%s]:%s\n"),_T("键名3"),V(_T("键名3")));
    _tprintf(_T("[%s]:%s\n"),_T("键名4"),V(_T("键名4")));
    return 0;
}
//ks[键名1|键名2|键名3]
//vs[键名1的值|键名2的值|键名3的值]
//[键名1]:键名1的值
//[键名2]:键名2的值
//[键名3]:键名3的值
//[键名4]:
//
轻箬笠 2018-06-07
  • 打赏
  • 举报
回复
function GetPrivateProfileString2(键名)

GetPrivateProfileString(节名,键名集合的名称,default,buf1,size);
GetPrivateProfileString(节名,键所对应值集合的名称,default,buf2,size);
std::vector<std::string> vec1,vec2;
vec1 = splitString(std::string(buf1), "|", true);
vec2 = splitString(std::string(buf2), "|", true);
std::vector<std::string>::iterator iter;
for (int index=0, iter=vec1.begin(); iter != vec1.end(); iter ++, index++)
{
std::string str = iter;
	if (键名==str)
	{
		return vec2[index];
	}
}
return "";
end

//下面这个函数是网上找的,地址是https://blog.csdn.net/u010236463/article/details/43307913
std::vector<std::string> splitString(std::string srcStr, std::string delimStr,bool repeatedCharIgnored)  
{  
    std::vector<std::string> resultStringVector;  
    std::replace_if(srcStr.begin(), srcStr.end(), [&](const char& c){if(delimStr.find(c)!=std::string::npos){return true;}else{return false;}}/*pred*/, delimStr.at(0));//将出现的所有分隔符都替换成为一个相同的字符(分隔符字符串的第一个)  
    size_t pos=srcStr.find(delimStr.at(0));  
    std::string addedString="";  
    while (pos!=std::string::npos) {  
        addedString=srcStr.substr(0,pos);  
        if (!addedString.empty()||!repeatedCharIgnored) {  
            resultStringVector.push_back(addedString);  
        }  
        srcStr.erase(srcStr.begin(), srcStr.begin()+pos+1);  
        pos=srcStr.find(delimStr.at(0));  
    }  
    addedString=srcStr;  
    if (!addedString.empty()||!repeatedCharIgnored) {  
        resultStringVector.push_back(addedString);  
    }  
    return resultStringVector;  
}  
yiyefangzhou24 2018-06-07
  • 打赏
  • 举报
回复
DWORD WINAPI GetPrivateProfileString( _In_ LPCTSTR lpAppName, _In_ LPCTSTR lpKeyName, _In_ LPCTSTR lpDefault, _Out_ LPTSTR lpReturnedString, _In_ DWORD nSize, _In_ LPCTSTR lpFileName ); GetPrivateProfileString function Retrieves a string from the specified section in an initialization file. Note This function is provided only for compatibility with 16-bit Windows-based applications. Applications should store initialization information in the registry. Syntax C++ DWORD WINAPI GetPrivateProfileString( _In_ LPCTSTR lpAppName, _In_ LPCTSTR lpKeyName, _In_ LPCTSTR lpDefault, _Out_ LPTSTR lpReturnedString, _In_ DWORD nSize, _In_ LPCTSTR lpFileName ); Parameters lpAppName [in] The name of the section containing the key name. If this parameter is NULL, the GetPrivateProfileString function copies all section names in the file to the supplied buffer. lpKeyName [in] The name of the key whose associated string is to be retrieved. If this parameter is NULL, all key names in the section specified by the lpAppName parameter are copied to the buffer specified by the lpReturnedString parameter. lpDefault [in] A default string. If the lpKeyName key cannot be found in the initialization file, GetPrivateProfileString copies the default string to the lpReturnedString buffer. If this parameter is NULL, the default is an empty string, "". Avoid specifying a default string with trailing blank characters. The function inserts a null character in the lpReturnedString buffer to strip any trailing blanks. lpReturnedString [out] A pointer to the buffer that receives the retrieved string. nSize [in] The size of the buffer pointed to by the lpReturnedString parameter, in characters. lpFileName [in] The name of the initialization file. If this parameter does not contain a full path to the file, the system searches for the file in the Windows directory. 第二个参数不就是键名,填入你想要读取的键名1/2/3????
API之网络函数1. API之网络函数 WNetAddConnection 创建同一个网络资源的永久性连接 WNetAddConnection2 创建同一个网络资源的连接 WNetAddConnection3 创建同一个网络资源的连接 WNetCancelConnection 结束一个网络连接 WNetCancelConnection2 结束一个网络连接 WNetCloseEnum 结束一次枚举操作 WNetConnectionDialog 启动一个标准对话框,以便建立同网络资源的连接 WNetDisconnectDialog 启动一个标准对话框,以便断开同网络资源的连接 WNetEnumResource 枚举网络资源 WNetGetConnection 获取本地或已连接的一个资源的网络名称 WNetGetLastError 获取网络错误的扩展错误信息 WNetGetUniversalName 获取网络中一个文件的远程名称以及/或者UNC(统一命名规范)名称 WNetGetUser 获取一个网络资源用以连接的名字 WNetOpenEnum 启动对网络资源进行枚举的过程 2. API之消息函数 BroadcastSystemMessage 将一条系统消息广播给系统中所有的顶级窗口 GetMessagePos 取得消息队列中上一条消息处理完毕时的鼠标指针屏幕位置 GetMessageTime 取得消息队列中上一条消息处理完毕时的时间 PostMessage 将一条消息投递到指定窗口的消息队列 PostThreadMessage 将一条消息投递给应用程序 RegisterWindowMessage 获取分配给一个字串标识符的消息编号 ReplyMessage 答复一个消息 SendMessage 调用一个窗口的窗口函数,将一条消息发给那个窗口 SendMessageCallback 将一条消息发给窗口 SendMessageTimeout 向窗口发送一条消息 SendNotifyMessage 向窗口发送一条消息 3. API之文件处理函数 CloseHandle 关闭一个内核对象。其中包括文件文件映射、进程、线程、安全和同步对象等 CompareFileTime 对比两个文件的时间 CopyFile 复制文件 CreateDirectory 创建一个新目录 CreateFile 打开和创建文件、管道、邮槽、通信服务、设备以及控制台 CreateFileMapping 创建一个新的文件映射对象 DeleteFile 删除指定文件 DeviceIoControl 对设备执行指定的操作 DosDateTimeToFileTime 将DOS日期和时间值转换成一个 win32 FILETIME 值 FileTimeToDosDateTime 将一个 win32 FILETIME 值转换成DOS日期和时间值 FileTimeToLocalFileTime 将一个FILETIME结构转换成本地时间 FileTimeToSystemTime 根据一个FILETIME结构的内容,装载一个SYSTEMTIME结构 FindClose 关闭由FindFirstFile函数创建的一个搜索句柄 FindFirstFile 根据文件名查找文件 FindNextFile 根据调用FindFirstFile函数时指定的一个文件名查找下一个文件 FlushFileBuffers 针对指定的文件句柄,刷新内部文件缓冲区 FlushViewOfFile 将写入文件映射缓冲区的所有数据都刷新到磁盘 GetBinaryType 判断文件是否可以执行 GetCompressedFileSize 判断一个压缩文件在磁盘上实际占据的字节数 GetCurrentDirectory 在一个缓冲区中装载当前目录 GetDiskFreeSpace 获取与一个磁盘的组织有关的信息,以及了解剩余空间的容量 GetDiskFreeSpaceEx 获取与一个磁盘的组织以及剩余空间容量有关的信息 GetDriveType 判断一个磁盘驱动器的类型 GetExpandedName 取得一个压缩文件的全名 GetFileAttributes 判断指定文件的属性 GetFileInformationByHandle 这个函数提供了获取文件信息的一种机制 GetFileSize 判断文件长度 GetFileTime 取得指定文件的时间信息 GetFileType 在给出文件句柄的前提下,判断文件类型 GetFileVersionInfo 从支持版本标记的一个模块里获取文件版本信息

3,881

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧