2010-11-15 45 views
0

我读文件行的内容我有下面的代码片段这是工作的罚款:如何使用行号

ifstream NDSConfig("NDS.config") ; 
     string szConfigVal ; 
     while(getline(NDSConfig, szConfigVal)) 
     { 
      //code 
     } 

但问题是,我需要通过比较行的值更新复选框状态。然后,代码将类似于以下:

ifstream NDSConfig("NDS.config") ; 
     string szConfigVal ; 
     while(getline(NDSConfig, szConfigVal)) 
     { 
      if(szConfigVal == "AutoStart = 1") 
      { 
         //Set Check Box True 
      } 
      else if(szConfigVal == "AutoStart = 0") 
      { 
         //Set Check Box False 
      } 

      if(szConfigVal == "AutLogHistory = 1") 
      { 
         //Set Check Box True 
      } 
      else if(szConfigVal == "AutLogHistory = 0") 
      { 
         //Set Check Box False 
      } 

      if(szConfigVal == "AutoScan = 1") 
      { 
         //Set Check Box True 
      } 
      else if(szConfigVal == "AutoScan = 0") 
      { 
         //Set Check Box False 
      } 

      if(szConfigVal == "AutoMount = 1") 
      { 
         //Set Check Box True 
      } 
      else if(szConfigVal == "AutoMount = 0") 
      { 
         //Set Check Box False 
      } 

      if(szConfigVal == "AutoOpen = 1") 
      { 
         //Set Check Box True 
      } 
      else if(szConfigVal == "AutoOpen = 0") 
      { 
         //Set Check Box False 
      } 

      if(szConfigVal == "LastConnectedSvr = 1") 
      { 
         //Set Check Box True 
      } 
      else if(szConfigVal == "LastConnectedSvr = 0") 
      { 
         //Set Check Box False 
      } 
     } 

如果我要while循环使用,然后我的状态将超过riden,只有最后的循环值或状态将被更新。有没有其他的出路。我需要从配置文件读取后设置复选框值。配置文件看起来如下:

自动启动= 0
AutLogHistory = 1
自动扫描= 1
AUTOMOUNT = 0
的AutoOpen = 0
LastConnectedSvr = 1

尽管我只有一个如果和其他一切,如果将帮助,但我需要一个更好的方式。

+1

目前尚不清楚问题所在。你是否希望NDS.config中的变量的第一个设置为“count”?你想检查文件中的双重变量吗?这与你在标题中提到的行号有什么关系? – 2010-11-15 11:03:57

回答

3

或者,使用boost::program_options,这是完全按照您的要求设计的!

编辑:更多的细节,program_options有一个方法来解析一个配置文件,比如你的,当你配置program_options时,你可以传入变量来存储配置文件中的值。他们的简单例子,它将变得非常清楚...

其他选项是让你存储你的密钥在地图上,默认值为0,然后当你解析你的文件时,将密钥设置为你从文件中读取值...

编辑:

使用程序选项(这是未经测试的代码,请尝试参考相关文档,并根据需要修复!)

int AutoStart; 
int AutLogHistory; 
int AutoScan; 
int AutoMount; 
int AutoOpen; 
int LastConnectedSvr; 

po::options_description desc("Allowed options"); 
desc.add_options() 
    ("help", "produce help message") 
    ("AutoStart", po::value<int>(&AutoStart)->default_value(0),"AutoStart") 
    ("AutLogHistory", po::value<int>(&AutLogHistory)->default_value(0),"AutLogHistory") 
    ("AutoScan", po::value<int>(&AutoScan)->default_value(0),"AutoScan") 
    ("AutoMount", po::value<int>(&AutoMount)->default_value(0),"AutoMount") 
    ("AutoOpen", po::value<int>(&AutoOpen)->default_value(0),"AutoOpen") 
    ("LastConnectedSvr", po::value<int>(&LastConnectedSvr)->default_value(0),"LastConnectedSvr") 
; 

std::ifstream config("NDS.config"); 

po::parse_command_line(config, desc, true); 

当该批次运行,各整数会有值从文件(或者被默认为0)。

这种方法的好处是你可以在你的配置文件中有不同的类型,只要它们被格式化为INI文件,那么这将起作用。

使用std ::地图另一种方法,以及@武果汁已经增加工作代码...

+0

如果你能详细说一下请问 – Simsons 2010-11-15 11:01:37

+0

@Subhen,详细说说哪一点? – Nim 2010-11-15 11:08:26

+0

如果您有任何示例代码片段。我没有找到在我的代码 – Simsons 2010-11-15 11:23:44

1

您可以使用boost program arguments parser使用专门config file parser让所有的程序参数。

这是提升的例子(小修改是我的):

#include <boost/program_options.hpp> 
namespace po = boost::program_options; 


#include <iostream> 
#include <fstream> 
#include <iterator> 
using namespace std; 

// A helper function to simplify the main part. 
template<class T> 
ostream& operator<<(ostream& os, const vector<T>& v) 
{ 
    copy(v.begin(), v.end(), ostream_iterator<T>(cout, " ")); 
    return os; 
} 


int main(int ac, char* av[]) 
{ 
     std::cout<<av[0]<<std::endl; 

std::vector<std::string> incsss; 
std::vector<std::string> filsss; 

    try { 
     int opt; 
     string config_file; 

     // Declare a group of options that will be 
     // allowed only on command line 
     po::options_description generic("Generic options"); 
     generic.add_options() 
      ("version,v", "print version string") 
      ("help,h", "produce help message") 
      ("config,c", po::value<string>(&config_file)->default_value("multiple_sources.cfg"), 
        "name of a file of a configuration.") 
      ; 

     // Declare a group of options that will be 
     // allowed both on command line and in 
     // config file 
     po::options_description config("Configuration"); 
     config.add_options() 
      ("optimization", po::value<int>(&opt)->default_value(10), 
        "optimization level") 
      ("include-path,I", 
       po::value< vector<string> >(&incsss)->composing(), 
       "include path") 
      ; 

     // Hidden options, will be allowed both on command line and 
     // in config file, but will not be shown to the user. 
     po::options_description hidden("Hidden options"); 
     hidden.add_options() 
     ("input-file", po::value< vector<string> >(&filsss), "input file") 
      ; 


     po::options_description cmdline_options; 
     cmdline_options.add(generic).add(config).add(hidden); 

     po::options_description config_file_options; 
     config_file_options.add(config).add(hidden); 

     po::options_description visible("Allowed options"); 
     visible.add(generic).add(config); 

     po::positional_options_description p; 
     p.add("input-file", 3); 

     po::variables_map vm; 
     store(po::command_line_parser(ac, av). 
       options(cmdline_options).positional(p).run(), vm); 
     notify(vm); 

     ifstream ifs(config_file.c_str()); 
     if (!ifs) 
     { 
       cout << "can not open config file: " << config_file << "\n"; 
      return 0; 
     } 
     else 
     { 
      store(parse_config_file(ifs, config_file_options), vm); 
      notify(vm); 
     } 

     if (vm.count("help")) { 
      cout << visible << "\n"; 
      cout<<"here"<<std::endl; 
      return 0; 
     } 

     if (vm.count("version")) { 
      cout << "Multiple sources example, version 1.0\n"; 
      return 0; 
     } 

     if (vm.count("include-path")) 
     { 
      cout << "Include paths are: " 
       << vm["include-path"].as< vector<string> >() << "\n"; 
     } 

     if (vm.count("input-file")) 
     { 
      cout << "Input files are: " 
       << vm["input-file"].as< vector<string> >() << "\n"; 
     } 

     cout << "Optimization level is " << opt << "\n"; 


     cout << "incsss constains : " <<incsss << std::endl; 
     cout << "filsss constains : " <<filsss << std::endl; 
    } 
    catch(exception& e) 
    { 
     cout << e.what() << "\n"; 
     return 1; 
    } 
    return 0; 
} 
2

您遇到的问题是在你的阅读在这一行的逻辑。你可以使用boost :: program_options如前面提到的,甚至是某种XML阅读器,但对于一个快速和肮脏的解决你的问题:现在

#include <string> 
#include <map> 
#include <algorithm> 

// typedef some stuff so our fingers don't die. 
typedef std::map<std::string, bool> CheckMap; 
typedef CheckMap::iterator CheckMap_it; 
typedef std::pair<std::string, bool> CheckPair; 

std::string szKey, szVal; 
// declare our map that will store the state 
CheckMap map; 


// insert possible config values 
map.insert(CheckPair("AutoStart", false)); 
map.insert(CheckPair("AutLogHistory", false)); 
map.insert(CheckPair("AutoScan", false)); 
map.insert(CheckPair("AutoMount", false)); 
map.insert(CheckPair("AutoOpen", false)); 
map.insert(CheckPair("LastConnectedSvr", false)); 

// now loop through the file 
ifstream NDSConfig("NDS.config") ; 
    string szConfigVal ; 
    while(getline(NDSConfig, szConfigVal)) 
    { 
     // erase any spaces in the line 
    szConfigVal.erase(std::remove_if(szConfigVal.begin(), szConfigVal.end(), isspace), szConfigVal.end()); 

     // locate equals, split in to <key> <value> using that. 
     std::string::size_type equals(szConfigVal.find('=')); 
    if(equals != std::string::npos) // exists? 
    { 
     szKey = szConfigVal.substr(0, equals); 
     szVal = szConfigVal.substr(equals + 1); 

     // locate key and set value 
     CheckMap_it it(map.find(szKey)); 
     if(it != map.end()) // sanity check 
     { 
     // could use a boost-type cast here, but I'm keeping it simple 
     it->second = (szVal == "1") ? true : false; 
     }; 
    }; 
    } 

,到了最后,在地图保存值与来自其中的文件的值。你可以遍历它们并且做你喜欢的事情。