關於ini檔案讀寫的一些問題

2021-07-07 04:49:57 字數 3737 閱讀 5645

[section1]

key1=***x 

key2=yyyy

[section2]

key1=***#adada#oooo

key2=yyy 

這是兩個配置段,第乙個配置段名為section,裡面有兩個配置專案即:key1,key2,值分別為***和yyyy要求能夠讀取這樣的檔案,並提供提取所有section名字列表的介面,提取訪問某個section下所有key的列表,提取訪問某個section下某個key的值的資訊(存為string型別)#為注釋部分的開頭可以出現先任何有效資訊之後,以及單列的一行,處理中應略去。

#ifndef _ini_file_  

#define _ini_file_  

#include

#include

#include

using namespace std;  

#define max_line_buf_size               80  

#define max_section_context_buf_size    40  

#define max_key_size                    40  

#define max_value_size                  40  

class inifile  

;  #endif

#include"inifile.h"

inifile::inifile( )  

inifile::~inifile()  

bool inifile::init( char* szfilename )  

m_strfilename = szfilename;  

file* pfile = fopen( szfilename, "rb" );  

if (null == pfile)  

char szreadlinebuf[max_line_buf_size];  

char szlinebuf[max_line_buf_size];  

string strcursection;  

string strkey;  

string strvalue;  

while(null != fgets(szreadlinebuf, max_line_buf_size, pfile))  

else if (isnewsection(szlinebuf))  

else if (iskeyvalueline(szlinebuf))  

else  

}  return true;  

}  bool inifile::isemptyline( char* szline )  

else  

}  bool inifile::isnewsection( char* szline )  

bool inifile::iskeyvalueline( char* szline )  

bool inifile::getnewsectioncontext( char* szline, string& strnewsectioncontext )  

;  strnewsectioncontext.clear();  

char* psectioncontextbegin = strchr(szline, '[');  

char* psectioncontextend = strchr(szline, ']');  

int nsectioncontextlen = psectioncontextend - psectioncontextbegin - 1;  

memcpy_s(szsectioncontextbuf, max_section_context_buf_size, psectioncontextbegin + 1, nsectioncontextlen);  

strnewsectioncontext = szsectioncontextbuf;  

return true;  

}  bool inifile::getkeyvalue( char* szline, string& strkey, string& strvalue )  

;  char szvaluebuf[max_value_size] = ;  

int nkeylen = pequalpos - szline;  

int nvaluelen = strlen(szline) - nkeylen - 1;  

memcpy_s(szkeybuf, max_key_size, szline, nkeylen);  

strkey = szkeybuf;  

memcpy_s(szvaluebuf, max_value_size, pequalpos + 1, nvaluelen);  

strvalue = szvaluebuf;  

return true;  

}  //void inifile::showfilecontext()  

//  

//        cout << "******************************" << endl;  

//        cout << endl;  

//    }  

//}  

void inifile::delinvalidsign( char* szoldline, char* sznewline )  

char tmpchar;  

int nnewlineindex = 0;  

for (int i = 0; i < ioldlinelen; i++)  

else

if(tmpchar=='#')

//++count;

tmpchar =szoldline[++i];

sznewline[nnewlineindex++] = tmpchar; 

}sznewline[nnewlineindex] = 0; 

}  string inifile::getsection()

return " ";

}string inifile::getkey(const string& strsection)

}return " ";

}string inifile::getvaluefromsection( const string& strsection, const string& strkey )  

mapkeyvalue& refkeyvaluemap = itsection->second;  

mapkeyvalue::iterator itkv = refkeyvaluemap.find(strkey);  

if (itkv != refkeyvaluemap.end())  

return " ";  

}  void inifile::showfilecontext()  

cout << "******************************" << endl;  

cout << endl;  

}  }  

#include "inifile.h"  

#include

#include

using namespace std;  

int main(int argc, char* argv)  

file.showfilecontext();  

cout}

關於面試的一些問題

面試過程中,面試官會向應聘者發問,而應聘者的回答將成為面試官考慮是否接受他的重要依據。對應聘者而言,了解這些問題背後的 貓膩 至關重要。本文對面試中經常出現的一些典型問題進行了整理,並給出相應的回答思路和參 讀者無需過分關注分析的細節,關鍵是要從這些分析中 悟 出面試的規律及回答問題的思維方式,達到...

關於Labview的一些問題

第一章 虛擬儀器及 labview入門 1 虛擬儀器概述 虛擬儀器 virtual instrumention 是基於計算機的儀器。計算機和儀器的密切結合是目前儀器發展的乙個重要方向。粗略地說這種結合有兩種方式,一種是將計算機裝入儀器,其典型的例子就是所謂智慧型化的儀器。隨著計算機功能的日益強大以及...

關於DropDownList的一些問題

dropdownlist選擇後提示不能在dropdownlist選擇多項 原因在於drop.selected true 用的是item的引用,無法覆蓋上次操作 有2種方法 一種是直接drop.selectvalue value 選定指定資料 另一種是在操作前 呼叫clearselection 方法清...