配置文件的读写.txt .dat .ini
来源:互联网 发布:java疯狂讲义版本 编辑:程序博客网 时间:2024/05/22 23:36
大多程序都会用到配置文件来动态读写一些东西,比较高级的点的就是xml,具体使用方法见我博客关于xml配置文件的文章。
这里主要为大家总结一下文本文档的配置文件读写,适合任何后缀,.txt .dat .ini .exe(修改txt) 甚至可以没有后缀,只要内容符合一定格式:
[section]
keyname1 = value1
keyname2 = value2
其余的格式自由发挥
以下是我用到的一个例子,后缀为.dat:
license.dat
#----------------------------------------------------------------------------------------------------# license 软件模块授权文件开始#----------------------------------------------------------------------------------------------------# 客户名称:航天XX研究所# 客户地址:北京市丰台区1234567# 联系人:ABC# 联系方式:DEF# 其他:服务器MAC:12-34-56-78-90-AB-CD服务器主硬盘ID:1234567890ABCDEFG#----------------------------------------------------------------------------------------------------# License 软件模块及授权信息#----------------------------------------------------------------------------------------------------[模块1]模块名称 = CableFastDesign数量 = 3有效期限 = 2015-12-31版本 = 2.X授权码 = 123456789EFGHJKLABCD[模块2]模块名称 = LAMCTools数量 = 4有效期限 = 2015-10-01版本 = 1.X授权码 = 123456789EFGHJKLABCD[模块3] /*section*/模块名称 = 3DDimension /*keyname = value nDefault是再找不到该keyname时的默认值*/ 数量 = 9有效期限 = 2015-10-01版本 = 1.X授权码 = 89EFGHJ1234567KLABCD#----------------------------------------------------------------------------------------------------# License 软件模块授权文件结束#----------------------------------------------------------------------------------------------------
该方法主要用到以下类型函数,大家可以到msdn中查找具体函数解释:
可以读取int、 float、 bool、 string类型变量,以读int为例:
UINT GetPrivateProfileInt( LPCTSTR lpAppName, // LPCTSTR lpKeyName, // key name INT nDefault, // return value if key name not found LPCTSTR lpFileName // initialization file name);
写入int、 float、 bool、 string类型变量,以string为例:
BOOL WritePrivateProfileString( LPCTSTR lpAppName, // section name LPCTSTR lpKeyName, // key name LPCTSTR lpString, // string to add LPCTSTR lpFileName // initialization file);
以下是以C++实现的源码,稍改以下C也可以:
IniReader.h
#ifndef INIREADER_H#define INIREADER_Hclass CIniReader{public:CIniReader(char* szFileName); int ReadInteger(char* szSection, char* szKey, int iDefaultValue);float ReadFloat(char* szSection, char* szKey, float fltDefaultValue);bool ReadBoolean(char* szSection, char* szKey, bool bolDefaultValue);char* ReadString(char* szSection, char* szKey, const char* szDefaultValue);private:char m_szFileName[255];};#endif//INIREADER_H
IniReader.cpp
#include "stdafx.h"#include "IniReader.h"#include <iostream>#include <Windows.h>CIniReader::CIniReader(char* szFileName){memset(m_szFileName, 0x00, 255);memcpy(m_szFileName, szFileName, strlen(szFileName));}int CIniReader::ReadInteger(char* szSection, char* szKey, int iDefaultValue){int iResult = GetPrivateProfileInt(szSection, szKey, iDefaultValue, m_szFileName); return iResult;}float CIniReader::ReadFloat(char* szSection, char* szKey, float fltDefaultValue){char szResult[255];char szDefault[255];float fltResult;sprintf(szDefault, "%f",fltDefaultValue);GetPrivateProfileString(szSection, szKey, szDefault, szResult, 255, m_szFileName); fltResult = atof(szResult);return fltResult;}bool CIniReader::ReadBoolean(char* szSection, char* szKey, bool bolDefaultValue){char szResult[255];char szDefault[255];bool bolResult;sprintf(szDefault, "%s", bolDefaultValue? "True" : "False");GetPrivateProfileString(szSection, szKey, szDefault, szResult, 255, m_szFileName); bolResult = (strcmp(szResult, "True") == 0 || strcmp(szResult, "true") == 0) ? true : false;return bolResult;}char* CIniReader::ReadString(char* szSection, char* szKey, const char* szDefaultValue){char* szResult = new char[255];memset(szResult, 0x00, 255);GetPrivateProfileString(szSection, szKey, szDefaultValue, szResult, 255, m_szFileName); return szResult;}
IniWriter.h
#ifndef INIWRITER_H#define INIWRITER_Hclass CIniWriter{public:CIniWriter(char* szFileName); void WriteInteger(char* szSection, char* szKey, int iValue);void WriteFloat(char* szSection, char* szKey, float fltValue);void WriteBoolean(char* szSection, char* szKey, bool bolValue);void WriteString(char* szSection, char* szKey, char* szValue);private:char m_szFileName[255];};#endif //INIWRITER_H
IniWriter.cpp
#include "stdafx.h"#include "IniWriter.h"#include <iostream>#include <Windows.h> CIniWriter::CIniWriter(char* szFileName){memset(m_szFileName, 0x00, 255);memcpy(m_szFileName, szFileName, strlen(szFileName));}void CIniWriter::WriteInteger(char* szSection, char* szKey, int iValue){char szValue[255];sprintf(szValue, "%d", iValue);WritePrivateProfileString(szSection, szKey, szValue, m_szFileName); }void CIniWriter::WriteFloat(char* szSection, char* szKey, float fltValue){char szValue[255];sprintf(szValue, "%f", fltValue);WritePrivateProfileString(szSection, szKey, szValue, m_szFileName); }void CIniWriter::WriteBoolean(char* szSection, char* szKey, bool bolValue){char szValue[255];sprintf(szValue, "%s", bolValue ? "True" : "False");WritePrivateProfileString(szSection, szKey, szValue, m_szFileName); }void CIniWriter::WriteString(char* szSection, char* szKey, char* szValue){WritePrivateProfileString(szSection, szKey, szValue, m_szFileName);}
如果有更好的方法请务必留下地址或者留言告知,互相学习
0 0
- 配置文件的读写.txt .dat .ini
- 读写ini配置文件的模块
- VC配置文件ini的读写
- VC配置文件ini的读写
- VC配置文件ini的读写
- VC配置文件ini的读写
- VC配置文件ini的读写
- Win32 ini配置文件的读写
- ini和txt文件的读写
- 读写INI配置文件的Java类
- 2个读写ini配置文件的函数
- VC配置文件ini的读写(转载)
- Autohotkey的配置文件读写方法ini
- bcb 读写ini 配置文件的一个例子
- 用于读写 INI 配置文件的 C++ 类
- VC配置文件ini的读写(转载)
- INI配置文件在linux下的读写
- C#对ini配置文件的读写
- 1013. 数素数
- 关于Python中定义类时 self 的理解
- 如何使用Photoshop(PS)将图片的底色变为透明
- 重拾Java回忆录(十二):Java12线程的使用
- 使用 Eclipse PhoneGap 构建 Android 应用程序入门
- 配置文件的读写.txt .dat .ini
- JPA/Hibernate注解的主键策略
- IO完成端口
- QML笔记整理——在Qt/C++应用中使用QML
- uva 10714 Ants
- Foundation框架(8)——NSMutableArray的增删改查、内存管理、排序
- Binary Tree Level Order Traversal
- win7系统开机设置及系统列表等待时间设置
- 输出格式控制