如何在VC++6.0中读取INI文件,帮忙 如何在VC++6.0中读取INI文件,谢谢,还有也要告诉我,怎么删除,更心,谢谢了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 GetPrivateProfileStringWritePrivateProfileString......参考msdn,里面介绍的十分详细 好像有个函数叫GetPrivateProfileInt...... GetProfileIntGetProfileStringWriteProfileIntWriteProfileString在WriteProfileXXX中lpszValue参数为NULL表示删除lpszEntry BOOL WritePrivateProfileString(LPCTSTR lpAppName,LPCTSTR lpKeyName,LPCTSTR lpString,LPCTSTR lpFileName); 其中各参数的意义: LPCTSTR lpAppName 是INI文件中的一个字段名. LPCTSTR lpKeyName 是lpAppName下的一个键名,通俗讲就是变量名. LPCTSTR lpString 是键值,也就是变量的值,不过必须为LPCTSTR型或CString型的. LPCTSTR lpFileName 是完整的INI文件名.//////////////////////////////CString strName,strTemp;int nAge;strName="张三";nAge=12;::WritePrivateProfileString("StudentInfo","Name",strName,"c:\\stud\\student.ini"); 此时c:\stud\student.ini文件中的内容如下: [StudentInfo] Name=张三要将学生的年龄保存下来,只需将整型的值变为字符型即可:strTemp.Format("%d",nAge);::WritePrivateProfileString("StudentInfo","Age",strTemp,"c:\\stud\\student.ini"); 读字符串:CString strStudName;int nStudAge; GetPrivateProfileString("StudentInfo","Name","默认姓名",strStudName.GetBuffer(MAX_PATH),MAX_PATH,"c:\\stud\\student.ini"); 执行后 strStudName 的值为:"张三",若前两个参数有误,其值为:"默认姓名".这里的参数意义与上相同.使用方法如下:读数值要用另一个函数:nStudAge=GetPrivateProfileInt("StudentInfo","Age",10,"c:\\stud\\student.ini"); http://www.chinaaskpro.com/2005412/2005412175133528.html写的很详细 #include "StdAfx.h" #include "ini.h" #define MAX_ALLSECTIONS 2048 //全部的段名 #define MAX_SECTION 260 //一个段名长度 #define MAX_ALLKEYS 6000 //全部的键名 #define MAX_KEY 260 //一个键名长度 BOOL CIni::SetFileName(LPCTSTR lpFileName) { CFile file; CFileStatus status; if(!file.GetStatus(lpFileName,status)) return TRUE; m_strFileName=lpFileName; return FALSE; } CString CIni::GetFileName(void) { return m_strFileName; } BOOL CIni::SetValue(LPCTSTR lpSection, LPCTSTR lpKey, LPCTSTR lpValue,bool bCreate) { TCHAR lpTemp[MAX_PATH] ={0}; //以下if语句表示如果设置bCreate为false时,当没有这个键名时则返回TRUE(表示出错) //!*&*none-value*&!* 这是个垃圾字符没有特别意义,这样乱写是防止凑巧相同。 if (!bCreate) { GetPrivateProfileString(lpSection,lpKey,"!*&*none-value*&!*",lpTemp,MAX_PATH,m_strFileName); if(strcmp(lpTemp,"!*&*none-value*&!*")==0) return TRUE; } if(WritePrivateProfileString(lpSection,lpKey,lpValue,m_strFileName)) return FALSE; else return GetLastError(); } CString CIni::GetValue(LPCTSTR lpSection, LPCTSTR lpKey) { DWORD dValue; TCHAR lpValue[MAX_PATH] ={0}; dValue=GetPrivateProfileString(lpSection,lpKey,"",lpValue,MAX_PATH,m_strFileName); return lpValue; } BOOL CIni::DelSection(LPCTSTR lpSection) { if(WritePrivateProfileString(lpSection,NULL,NULL,m_strFileName)) return FALSE; else return GetLastError(); } BOOL CIni::DelKey(LPCTSTR lpSection, LPCTSTR lpKey) { if(WritePrivateProfileString(lpSection,lpKey,NULL,m_strFileName)) return FALSE; else return GetLastError(); } int CIni::GetSections(CStringArray& arrSection) { /* 本函数基础: GetPrivateProfileSectionNames - 从 ini 文件中获得 Section 的名称 如果 ini 中有两个 Section: [sec1] 和 [sec2],则返回的是 'sec1',0,'sec2',0,0 ,当你不知道 ini 中有哪些 section 的时候可以用这个 api 来获取名称 */ int i; int iPos=0; int iMaxCount; TCHAR chSectionNames[MAX_ALLSECTIONS]={0}; //总的提出来的字符串 TCHAR chSection[MAX_SECTION]={0}; //存放一个段名。 GetPrivateProfileSectionNames(chSectionNames,MAX_ALLSECTIONS,m_strFileName); //以下循环,截断到两个连续的0 for(i=0;i<MAX_ALLSECTIONS;i++) { if (chSectionNames[i]==0) if (chSectionNames[i]==chSectionNames[i+1]) break; } iMaxCount=i+1; //要多一个0号元素。即找出全部字符串的结束部分。 arrSection.RemoveAll();//清空原数组 for(i=0;i<iMaxCount;i++) { chSection[iPos++]=chSectionNames[i]; if(chSectionNames[i]==0) { arrSection.Add(chSection); memset(chSection,0,MAX_SECTION); iPos=0; } } return (int)arrSection.GetSize(); } int CIni::GetKeyValues(CStringArray& arrKey,CStringArray& arrValue, LPCTSTR lpSection) { /* 本函数基础: GetPrivateProfileSection- 从 ini 文件中获得一个Section的全部键名及值名 如果ini中有一个段,其下有 "段1=值1" "段2=值2",则返回的是 '段1=值1',0,'段2=值2',0,0 ,当你不知道 获得一个段中的所有键及值可以用这个。 */ int i; int iPos=0; CString strKeyValue; int iMaxCount; TCHAR chKeyNames[MAX_ALLKEYS]={0}; //总的提出来的字符串 TCHAR chKey[MAX_KEY]={0}; //提出来的一个键名 GetPrivateProfileSection(lpSection,chKeyNames,MAX_ALLKEYS,m_strFileName); for(i=0;i<MAX_ALLKEYS;i++) { if (chKeyNames[i]==0) if (chKeyNames[i]==chKeyNames[i+1]) break; } iMaxCount=i+1; //要多一个0号元素。即找出全部字符串的结束部分。 arrKey.RemoveAll();//清空原数组 arrValue.RemoveAll(); for(i=0;i<iMaxCount;i++) { chKey[iPos++]=chKeyNames[i]; if(chKeyNames[i]==0) { strKeyValue=chKey; arrKey.Add(strKeyValue.Left(strKeyValue.Find("="))); arrValue.Add(strKeyValue.Mid(strKeyValue.Find("=")+1)); memset(chKey,0,MAX_KEY); iPos=0; } } return (int)arrKey.GetSize(); } BOOL CIni::DelAllSections() { int nSection; CStringArray arrSection; nSection=GetSections(arrSection); for(int i=0;i<nSection;i++) { if(DelSection(arrSection[i])) return GetLastError(); } return FALSE; } 在MFC中利用ADO访问SQL Server 2008如何使用多线程,插入读取数据 一个对话框的问题,有点难! 设置了个窗口消息WindowProc无效!跪求啊! 关于在Win32 Application中使用MFC的问题 怎么样得到Edit中的内容 请教:调用dll的一个问题 用ODBC API怎么进行记录插入?? 如何以最快的速度从递归程序中跳出。。。。。。。。。。。。。。 问:如何实现“单击鼠标左键,等效于使键盘上的F7被按下一次”? fopen打不开文件 vc里可有显示坐标轴的控件? 如何程序一运行,就读取INI文件
WritePrivateProfileString......
参考msdn,里面介绍的十分详细
GetProfileString
WriteProfileInt
WriteProfileString在WriteProfileXXX中lpszValue参数为NULL表示删除lpszEntry
LPCTSTR lpAppName,
LPCTSTR lpKeyName,
LPCTSTR lpString,
LPCTSTR lpFileName
);
其中各参数的意义:
LPCTSTR lpAppName 是INI文件中的一个字段名.
LPCTSTR lpKeyName 是lpAppName下的一个键名,通俗讲就是变量名.
LPCTSTR lpString 是键值,也就是变量的值,不过必须为LPCTSTR型或CString型的.
LPCTSTR lpFileName 是完整的INI文件名.
//////////////////////////////
CString strName,strTemp;
int nAge;
strName="张三";
nAge=12;
::WritePrivateProfileString("StudentInfo","Name",strName,"c:\\stud\\student.ini");
此时c:\stud\student.ini文件中的内容如下:
[StudentInfo]
Name=张三
要将学生的年龄保存下来,只需将整型的值变为字符型即可:
strTemp.Format("%d",nAge);
::WritePrivateProfileString("StudentInfo","Age",strTemp,"c:\\stud\\student.ini");
读字符串:
CString strStudName;
int nStudAge;
GetPrivateProfileString("StudentInfo","Name","默认姓名",strStudName.GetBuffer(MAX_PATH),MAX_PATH,"c:\\stud\\student.ini");
执行后 strStudName 的值为:"张三",若前两个参数有误,其值为:"默认姓名".
这里的参数意义与上相同.使用方法如下:
读数值要用另一个函数:
nStudAge=GetPrivateProfileInt("StudentInfo","Age",10,"c:\\stud\\student.ini");
写的很详细
#include "StdAfx.h"
#include "ini.h" #define MAX_ALLSECTIONS 2048 //全部的段名
#define MAX_SECTION 260 //一个段名长度
#define MAX_ALLKEYS 6000 //全部的键名
#define MAX_KEY 260 //一个键名长度 BOOL CIni::SetFileName(LPCTSTR lpFileName)
{
CFile file;
CFileStatus status; if(!file.GetStatus(lpFileName,status))
return TRUE; m_strFileName=lpFileName;
return FALSE;
} CString CIni::GetFileName(void)
{
return m_strFileName;
} BOOL CIni::SetValue(LPCTSTR lpSection, LPCTSTR lpKey, LPCTSTR lpValue,bool bCreate)
{
TCHAR lpTemp[MAX_PATH] ={0}; //以下if语句表示如果设置bCreate为false时,当没有这个键名时则返回TRUE(表示出错)
//!*&*none-value*&!* 这是个垃圾字符没有特别意义,这样乱写是防止凑巧相同。
if (!bCreate)
{
GetPrivateProfileString(lpSection,lpKey,"!*&*none-value*&!*",lpTemp,MAX_PATH,m_strFileName);
if(strcmp(lpTemp,"!*&*none-value*&!*")==0)
return TRUE;
} if(WritePrivateProfileString(lpSection,lpKey,lpValue,m_strFileName))
return FALSE;
else
return GetLastError();
} CString CIni::GetValue(LPCTSTR lpSection, LPCTSTR lpKey)
{
DWORD dValue;
TCHAR lpValue[MAX_PATH] ={0}; dValue=GetPrivateProfileString(lpSection,lpKey,"",lpValue,MAX_PATH,m_strFileName);
return lpValue;
} BOOL CIni::DelSection(LPCTSTR lpSection)
{
if(WritePrivateProfileString(lpSection,NULL,NULL,m_strFileName))
return FALSE;
else
return GetLastError();
} BOOL CIni::DelKey(LPCTSTR lpSection, LPCTSTR lpKey)
{
if(WritePrivateProfileString(lpSection,lpKey,NULL,m_strFileName))
return FALSE;
else
return GetLastError();
}
int CIni::GetSections(CStringArray& arrSection)
{
/*
本函数基础:
GetPrivateProfileSectionNames - 从 ini 文件中获得 Section 的名称
如果 ini 中有两个 Section: [sec1] 和 [sec2],则返回的是 'sec1',0,'sec2',0,0 ,当你不知道
ini 中有哪些 section 的时候可以用这个 api 来获取名称
*/
int i;
int iPos=0;
int iMaxCount;
TCHAR chSectionNames[MAX_ALLSECTIONS]={0}; //总的提出来的字符串
TCHAR chSection[MAX_SECTION]={0}; //存放一个段名。
GetPrivateProfileSectionNames(chSectionNames,MAX_ALLSECTIONS,m_strFileName); //以下循环,截断到两个连续的0
for(i=0;i<MAX_ALLSECTIONS;i++)
{
if (chSectionNames[i]==0)
if (chSectionNames[i]==chSectionNames[i+1])
break;
} iMaxCount=i+1; //要多一个0号元素。即找出全部字符串的结束部分。
arrSection.RemoveAll();//清空原数组 for(i=0;i<iMaxCount;i++)
{
chSection[iPos++]=chSectionNames[i];
if(chSectionNames[i]==0)
{
arrSection.Add(chSection);
memset(chSection,0,MAX_SECTION);
iPos=0;
} } return (int)arrSection.GetSize();
} int CIni::GetKeyValues(CStringArray& arrKey,CStringArray& arrValue, LPCTSTR lpSection)
{
/*
本函数基础:
GetPrivateProfileSection- 从 ini 文件中获得一个Section的全部键名及值名
如果ini中有一个段,其下有 "段1=值1" "段2=值2",则返回的是 '段1=值1',0,'段2=值2',0,0 ,当你不知道
获得一个段中的所有键及值可以用这个。
*/
int i;
int iPos=0;
CString strKeyValue;
int iMaxCount;
TCHAR chKeyNames[MAX_ALLKEYS]={0}; //总的提出来的字符串
TCHAR chKey[MAX_KEY]={0}; //提出来的一个键名 GetPrivateProfileSection(lpSection,chKeyNames,MAX_ALLKEYS,m_strFileName); for(i=0;i<MAX_ALLKEYS;i++)
{
if (chKeyNames[i]==0)
if (chKeyNames[i]==chKeyNames[i+1])
break;
} iMaxCount=i+1; //要多一个0号元素。即找出全部字符串的结束部分。
arrKey.RemoveAll();//清空原数组
arrValue.RemoveAll(); for(i=0;i<iMaxCount;i++)
{
chKey[iPos++]=chKeyNames[i];
if(chKeyNames[i]==0)
{
strKeyValue=chKey;
arrKey.Add(strKeyValue.Left(strKeyValue.Find("=")));
arrValue.Add(strKeyValue.Mid(strKeyValue.Find("=")+1));
memset(chKey,0,MAX_KEY);
iPos=0;
} } return (int)arrKey.GetSize();
} BOOL CIni::DelAllSections()
{
int nSection;
CStringArray arrSection;
nSection=GetSections(arrSection);
for(int i=0;i<nSection;i++)
{
if(DelSection(arrSection[i]))
return GetLastError();
}
return FALSE;
}