linux/windows 讀寫ini配置檔案
DWORD MyClass::GetPrivateProfileString(
LPCTSTR lpAppName, // section name
LPCTSTR lpKeyName, // key name
LPCTSTR lpDefault, // default string
LPTSTR lpReturnedString, // destination buffer
DWORD nSize, // size of destination buffer
LPCTSTR lpFileName // initialization file name
)
{
int iRet = -1;
//WINDOWS編譯環境
#ifdef WIN32
return ::GetPrivateProfileString(lpAppName, lpKeyName, lpDefault, lpReturnedString, nSize, lpFileName);
//linux編譯環境
#elif defined LINUX
short bfindsection = false;
char szsection[100] = {0};
char szentry[100] = {0};
char sztmp[MAXLINELENGTH] = {0};
char sztmpb[MAXLINELENGTH] = {0};
char szresult[MAXLINELENGTH] = {0};
memset(lpReturnedString, 0, strlen(lpReturnedString));
//去右空格
//TrimRight(lpAppName);
//TrimRight(lpKeyName);
//TrimRight(lpFileName);
if(false == CAdapter::PathFileExists(lpFileName))
{
strcpy(lpReturnedString,lpDefault);
return strlen(lpDefault);
}
sprintf(szsection , "[%s]" , lpAppName);
sprintf(szentry , "%s=" , lpKeyName);
FILE* stream;
stream = fopen( lpFileName, "r" );
if( stream != NULL )
{
//找到pcsection
while(!feof(stream) && !bfindsection)
{
memset(sztmp,0x00,MAXLINELENGTH);
fscanf(stream , "%s" , sztmp) ;
bfindsection = !strcmp(sztmp , szsection) ;
}
memset(sztmp,0x00,MAXLINELENGTH);
while (!feof(stream) && bfindsection && sztmp[0] != '[')
{
memset(sztmp,0x00,sizeof(sztmp));
ReadString(sztmp , MAXLINELENGTH, stream);
//RemoveChar(sztmp,' ');
//printf("%s\n", sztmp);
RemoveSpace(sztmp);
//printf("%s\n\n", sztmp);
strncpy(sztmpb, sztmp, sizeof(sztmpb));
if (strncmp(sztmp,szentry,strlen(szentry)) == 0)
{
strncpy(szresult , &sztmpb[strlen(szentry)], sizeof(szresult)-1);
//RemoveChar(szresult,'=');
RemoveChar(szresult,'\n');
RemoveChar(szresult,'\r');
if(nSize < strlen(szresult) && lpReturnedString != NULL)
{
strncpy(lpReturnedString, szresult, nSize-1);
lpReturnedString[nSize-1] = '\0';
iRet = nSize-1;
}
else if(lpReturnedString != NULL)
{
strncpy(lpReturnedString, szresult, strlen(szresult));
iRet = strlen(lpReturnedString);
}
break;
}
}
}
//取預設值的情況:
//1.開啟檔案失敗
//2.AppName未找到
//3.KeyName未找到
if(((NULL == stream) || !bfindsection || (sztmp[0] == '[') || (!feof(stream) && iRet==-1)) && (lpDefault != NULL))
{
strncpy(lpReturnedString, lpDefault, nSize);
iRet = strlen(lpDefault);
}
if(stream != NULL)
{
fclose( stream );
stream = NULL;
}
#endif
return iRet;
}
//按行讀取檔案,去除行中的'\r'和'\n'
char* MyClass::ReadFileLine(char* szConn, FILE* stream)
{
char* pc = szConn;
char conn[2] = {0};
while (fread(conn, 1, 1, stream) > 0)
{
if (conn[0] == '\r')
continue;
if (conn[0] == '\n')
break;
memcpy(pc, conn, 1);
++pc;
}
*pc = '\0';
return szConn;
}
bool MyClass::WritePrivateProfileString( LPCSTR lpAppName, LPCSTR lpKeyName, LPCSTR lpString, LPCSTR lpFileName )
{
#ifdef WIN32
return ::WritePrivateProfileString(lpAppName, lpKeyName, lpString, lpFileName);
#elif defined LINUX
char szsection[100] = {0};
char szentry[100] = {0};
char sztmp[MAXLINELENGTH] = {0};
sprintf(szsection, "[%s]", lpAppName);
sprintf(szentry , "%s=" , lpKeyName);
//read file conn
if (!PathFileExists(lpFileName))
{
//file not exist
FILE* pfile = fopen(lpFileName, "w");
if (!pfile)
{
return false;
}
sprintf(sztmp, "%s\n%s%s\n", szsection, szentry, lpString);
fwrite(sztmp, sizeof(char), strlen(sztmp), pfile);
fclose(pfile);
return true;
}
std::string strConn = "";
std::string strRow = "";
size_t nAppPos = std::string::npos;
size_t nKeyPos = 0;
bool bfindapp = true;
bool bfindkey = false;
FILE* pfile = NULL;
pfile = fopen(lpFileName, "r");
while (!feof(pfile))
{
memset(sztmp, 0, sizeof(sztmp));
fscanf(pfile, "%s", sztmp); //行讀取 確保每行無空格以\n結尾,否則需自定義函式
strRow = sztmp;
if (nAppPos == std::string::npos && (nAppPos = strRow.find(szsection)) == 0)
{
strConn += szsection;
strConn += "\n";
nAppPos = strConn.length();
nKeyPos = std::string::npos;
}
else if (nKeyPos == std::string::npos && (nKeyPos = strRow.find(szentry)) == 0)
{
strConn += szentry;
strConn += lpString;
strConn += "\n";
}
else
{
strConn += strRow;
strConn += "\n";
}
}
if (nAppPos == std::string::npos && nKeyPos == 0)
{
memset(sztmp, 0, sizeof(sztmp));
sprintf(sztmp, "%s\n%s%s\n", szsection, szentry, lpString);
strConn += sztmp;
}
else if (nAppPos != std::string::npos && nKeyPos == std::string::npos)
{
std::string strBack = strConn.substr(nAppPos);
strConn = strConn.substr(0, nAppPos);
strConn += szentry;
strConn += lpString;
strConn += "\n" + strBack;
}
fclose(pfile);
pfile = fopen(lpFileName, "w");
fwrite(strConn.c_str(), sizeof(char), strConn.length(), pfile);
fclose(pfile);
return true;
#endif
}
相關文章
- java 讀寫 ini 配置檔案Java
- C#中讀寫INI配置檔案C#
- 使用IniEditor讀寫INI型別配置檔案型別
- c#讀寫ini檔案C#
- VB讀寫ini檔案 (轉)
- go 讀取.ini配置檔案Go
- Linux下用C讀取INI配置檔案Linux
- VB.NET 讀寫ini檔案
- 使用C#讀寫ini檔案C#
- C#讀取ini配置檔案C#
- C#關於讀寫INI檔案C#
- C# winform中讀寫ini檔案C#ORM
- Python常用配置檔案ini、json、yaml讀寫總結PythonJSONYAML
- 透過python讀取ini配置檔案Python
- 讀寫INI檔案的四個函式 (轉)函式
- NPM酷庫047:ini,解析INI配置檔案NPM
- Python讀取修改ini配置檔案[ConfigParser]Python
- mysql--my.ini配置檔案配置MySql
- winform c#寫ini檔案ORMC#
- 建立與讀取.ini檔案
- delphi讀取ini檔案 (轉)
- Mysql 配置檔案 my.iniMySql
- MySQL檔案my.ini配置MySql
- pyinstaller 打包後讀取 ini 配置檔案路徑錯誤,怎麼定位配置檔案
- MySQL配置檔案my.ini在哪MySql
- python ini 配置檔案處理Python
- MySql5.7配置檔案my.ini 設定 my.ini檔案路徑MySql
- Linux核心中讀寫檔案Linux
- Mysql配置檔案my.ini配置項詳解MySql
- PHP配置檔案詳解php.iniPHP
- pytest配置檔案pytest.ini
- Python之ini配置檔案詳解Python
- MySQL 配置檔案 (my.ini) 詳解MySql
- asp.net 操作INI配置檔案類ASP.NET
- Mysql my.ini配置檔案詳解MySql
- Mysql配置檔案:my.ini詳解MySql
- API讀取寫入 ini檔案內容的方法函式詳解API函式
- linux讀寫檔案 簡單版Linux