<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                想項目開發中終歸會用到配置文件,方便改服務器IP端口這些 這個代碼適合Key-Value鍵值對類型的配置文件,我的配置文件是這樣的 ~~~ VideoServerAddress = 192.168.0.139 VideoServerPort = 8000 VideoClientPort = 2000 LandServerAddress = 183.61.39.156 LandSserverPort = 6041 ~~~ 下面的這份代碼我也是拷貝的,然后稍微改造了一下,我運行試了試,確實可以讀配置文件。 首先是h文件 ~~~ /************************************** *日期: 2014-12-5 *目的: 讀取配置文件的信息,以map的形式存入 *要求: 配置文件的格式,以#作為行注釋,配置的形式是key = value,中間可有空 格,也可沒有空格 ***************************************/ #ifndef _GET_CONFIG_H_ #define _GET_CONFIG_H_ #define COMMENT_CHAR '#' #include <string> #include <map> using namespace std; bool ReadConfig(const string & filename, map<string, string> & m); void PrintConfig(const map<string, string> & m); #endif ~~~ 然后就是Cpp了,重頭。 ~~~ #include "GetConfig.h" #include <fstream> #include <iostream> using namespace std; bool IsSpace(char c) { if (' ' == c || '\t' == c) return true; return false; } bool IsCommentChar(char c) { if (c == COMMENT_CHAR){ return true; }else{ return false; } } void Trim(string & str) { if (str.empty()) { return; } int i, start_pos, end_pos; for (i = 0; i < str.size(); ++i) { if (!IsSpace(str[i])) { break; } } if (i == str.size()) { // 全部是空白字符串 str = ""; return; } start_pos = i; for (i = str.size() - 1; i >= 0; --i) { if (!IsSpace(str[i])) { break; } } end_pos = i; str = str.substr(start_pos, end_pos - start_pos + 1); } bool AnalyseLine(const string & line, string & key, string & value) { if (line.empty()) return false; int start_pos = 0, end_pos = line.size() - 1, pos; if ((pos = line.find(COMMENT_CHAR)) != -1) { if (0 == pos) { // 行的第一個字符就是注釋字符 return false; } end_pos = pos - 1; } string new_line = line.substr(start_pos, start_pos + 1 - end_pos); // 預處理,刪除注釋部分 if ((pos = new_line.find('=')) == -1) return false; // 沒有=號 key = new_line.substr(0, pos); value = new_line.substr(pos + 1, end_pos + 1- (pos + 1)); Trim(key); if (key.empty()) { return false; } Trim(value); return true; } // 讀取數據 bool ReadConfig(const string & filename, map<string, string> & m) { m.clear(); ifstream infile(filename.c_str()); if (!infile) { cout << "file open error" << endl; return false; } string line, key, value; while (getline(infile, line)) { if (AnalyseLine(line, key, value)) { m[key] = value; } } infile.close(); return true; } // 打印讀取出來的數據 void PrintConfig(const map<string, string> & m) { //ofstream fout("e:\\Config.txt"); map<string, string>::const_iterator mite = m.begin(); for (; mite != m.end(); ++mite) { cout << mite->first << "=" << mite->second << endl; /*// 把內容輸出到新的文件里面去 fout << mite->first << "=" << mite->second << endl;*/ } //fout.close(); } int main() { string m_sPath = "e:\\LandClient.conf"; map<string,string> m_mapConfig; ReadConfig(m_sPath,m_mapConfig); PrintConfig(m_mapConfig); system("pause"); return 0; } ~~~ 這份代碼我覺得精髓就在與stl的運用,確實不錯。 下面是運行的結果: ![](https://box.kancloud.cn/2016-08-19_57b6ce7940bed.jpg) 對了 ?這是原文的鏈接:http://www.cnblogs.com/tzhangofseu/archive/2011/10/02/2197966.html 感覺這份代碼的函數封裝以及map的運用確實不錯
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看