我用qt库写的两个静态方法,writePrivateProfileString功能比较全了,getPrivateProfileString功能只做了比较实用的。
//************************************
// Method: WritePrivateProfileString
// Usage: strValue是null则删除key,key是null则删除section,section是null直接返回。writeprivateprofilestring
// FullName: MyClass::WritePrivateProfileString
// Access: public
// Returns: bool
// Parameter: QString strSectionName
// Parameter: QString strKeyName
// Parameter: QString strValue
// Parameter: QString strFileName
//************************************
bool MyClass::WritePrivateProfileString(QString strSectionName, QString strKeyName, QString strValue, QString strFileName)
{
bool ret = false;
//section是null则直接返回false
if(strSectionName == NULL)
{
return ret;
}
bool flagFindSection = false;//是否找到了section
int pos = 0;
QString strSection("[" + strSectionName + "]");
QString strKey(strKeyName + "=");
//文件不存在,则创建,且直接写入
if(!QFile::exists(strFileName))
{
QFile createFile(strFileName);
if(!createFile.open(QFile::WriteOnly | QIODevice::Text))
{
return ret;
}
//如果key和value不是null,才写内容
if(strKeyName != NULL && strValue != NULL)
{
QTextStream createStream(&createFile);
createStream<<strSection<<"/n";
createStream<<strKey<<strValue<<"/n";
createStream.flush();
ret = true;
}
createFile.close();
return ret;
}
QFile readFile(strFileName);
if (!readFile.open(QFile::ReadOnly | QIODevice::Text))
{
ret = false;
return ret;
}
QFile writeFile(strFileName);
//读入流和写入流
//写入流文件在最后才WriteOnly打开
QTextStream readStream(&readFile);
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-27851-1.html
为何有关部门不出来表态