linux 配置檔案讀寫解析

2021-08-17 08:35:52 字數 1571 閱讀 6940

有時我們自己寫linux下配置檔案,需要手動解析,然後使得程式可以配置的

類似於以下格式 ******.conf:

## sample configuration file
ip=192.16.31.2

position = centered

直接上**解析:

標頭檔案ccl.h

#ifndef cll_kvconf_h

#define cll_kvconf_h

#define keyvallen 256

/* 刪除左邊的空格 */

char * l_trim(char * szoutput, const char *szinput);

/* 刪除右邊的空格 */

char *r_trim(char *szoutput, const char *szinput);

/* 刪除兩邊的空格 */

char * a_trim(char * szoutput, const char * szinput);

#endif //cll_kvconf_h

原始檔ccl.cpp

#include #include #include #include #include #include #include "kvconf.h"

/* 刪除左邊的空格 */

char * l_trim(char * szoutput, const char *szinput)

return strcpy(szoutput, szinput);

}/* 刪除右邊的空格 */

char *r_trim(char *szoutput, const char *szinput)

*(++p) = '\0';

return szoutput;

}/* 刪除兩邊的空格 */

char * a_trim(char * szoutput, const char * szinput)

*(++p) = '\0';

return szoutput;

}int getprofilestring(char *profile, char *keyname, char *keyval )

fseek( fp, 0, seek_set );

while( !feof(fp) && fgets( buf_i, keyvallen, fp )!=null ) else

found = 2;

break;

} else

}// }

}fclose( fp );

if( found == 2 )

return(0);

else

return(-1);

}int getprofileint(char *profile, char *keyname, int *keyval )

int main()

讀寫配置檔案

windows作業系統專門為此提供了6個api函式來對配置設定檔案進行讀 寫 getprivateprofileint 從私有初始化檔案獲取整型數值 getprivateprofilestring 從私有初始化檔案獲取字串型值 getprofileint 從win.ini 獲取整數值 getprof...

讀寫配置檔案

procedure curstepchanged curstep tsetupstep varsetupname string inipath string begin case curstep of ssinstall 複製檔案前 begin end sspostinstall 完成複製 begi...

讀寫配置檔案

寫配置檔案 include include include include using std cout using std endl int main int nlen strlen d myinifile.ini 16 nlen strlen lppath 0 strcpy s lppath,s...