Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 读取配置文件_C++ - Fatal编程技术网

C++ 读取配置文件

C++ 读取配置文件,c++,C++,我正在尝试读取如下配置文件: rawfile=input.raw encfile=encoded.enc decfile=decoded.raw width=512 height=512 rle=1 quantfile=matrix.txt logfile=log.txt 具有此功能: void Compression::readConfigFile(char * input) { string lineBuf; string optionBuf; std::ifst

我正在尝试读取如下配置文件:

rawfile=input.raw
encfile=encoded.enc
decfile=decoded.raw
width=512
height=512
rle=1
quantfile=matrix.txt 
logfile=log.txt
具有此功能:

void Compression::readConfigFile(char * input)
{
    string lineBuf;
    string optionBuf;
    std::ifstream confFile(input);

    if ( confFile.is_open() )
    {
        while ( getline( confFile, lineBuf ) )
        {
            optionBuf = "rawfile=";
            if ( ( ( int )lineBuf.find(optionBuf) ) != -1 )
            {
                lineBuf.erase( 0, optionBuf.length() );
                this->rawfile = lineBuf.c_str();
            }
            optionBuf = "encfile=";
            if ( ( ( int )lineBuf.find(optionBuf) ) != -1 )
            {
                lineBuf.erase( 0, optionBuf.length() );
                this->encfile = lineBuf.c_str();
            }
            optionBuf = "decfile=";
            if ( ( ( int )lineBuf.find(optionBuf) ) != -1 )
            {
                lineBuf.erase( 0, optionBuf.length() );
                this->encfile = lineBuf.c_str();
            }
            optionBuf = "width=";
            if ( ( ( int )lineBuf.find(optionBuf) ) != -1 )
            {
                lineBuf.erase( 0, optionBuf.length() );
                this->width = atoi( lineBuf.c_str() );
            }
            optionBuf = "height=";
            if ( ( ( int )lineBuf.find(optionBuf) ) != -1 )
            {
                lineBuf.erase( 0, optionBuf.length() );
                this->height = atoi( lineBuf.c_str() );
            }
            optionBuf = "rle=";
            if ( ( ( int )lineBuf.find(optionBuf) ) != -1 )
            {
                lineBuf.erase( 0, optionBuf.length() );
                this->rle = atoi( lineBuf.c_str() );
            }
            optionBuf = "quantfile=";
            if ( ( ( int )lineBuf.find(optionBuf) ) != -1 )
            {
                lineBuf.erase( 0, optionBuf.length());
                this->matrix = lineBuf.c_str();
            }
            optionBuf = "logfile=";
            if ( ( ( int )lineBuf.find(optionBuf) ) != -1 )
            {
                lineBuf.erase( 0, optionBuf.length() );
                this->logfile = lineBuf.c_str();
            }
            confFile.close();

        }
    }
    else
        cout << "Can't open file: " << input << endl;
}
void压缩::readConfigFile(char*input)
{
字符串lineBuf;
字符串选项buf;
std::ifstream confFile(输入);
if(confFile.is_open())
{
while(getline(confFile,lineBuf))
{
optionBuf=“rawfile=”;
if(((int)lineBuf.find(optionBuf))!=-1)
{
lineBuf.erase(0,optionBuf.length());
这->rawfile=lineBuf.c_str();
}
optionBuf=“encfile=”;
if(((int)lineBuf.find(optionBuf))!=-1)
{
lineBuf.erase(0,optionBuf.length());
此->encfile=lineBuf.c_str();
}
optionBuf=“decfile=”;
if(((int)lineBuf.find(optionBuf))!=-1)
{
lineBuf.erase(0,optionBuf.length());
此->encfile=lineBuf.c_str();
}
optionBuf=“width=”;
if(((int)lineBuf.find(optionBuf))!=-1)
{
lineBuf.erase(0,optionBuf.length());
this->width=atoi(lineBuf.c_str());
}
optionBuf=“height=”;
if(((int)lineBuf.find(optionBuf))!=-1)
{
lineBuf.erase(0,optionBuf.length());
这个->高度=atoi(lineBuf.c_str());
}
optionBuf=“rle=”;
if(((int)lineBuf.find(optionBuf))!=-1)
{
lineBuf.erase(0,optionBuf.length());
这->rle=atoi(lineBuf.c_str());
}
optionBuf=“quantfile=”;
if(((int)lineBuf.find(optionBuf))!=-1)
{
lineBuf.erase(0,optionBuf.length());
这->矩阵=lineBuf.c_str();
}
optionBuf=“logfile=”;
if(((int)lineBuf.find(optionBuf))!=-1)
{
lineBuf.erase(0,optionBuf.length());
这->日志文件=lineBuf.c_str();
}
confFile.close();
}
}
其他的

难道你不应该在while循环之外关闭文件吗

while() {
    ...
}
confFile.close();

你不应该在while循环之外关闭文件吗

while() {
    ...
}
confFile.close();

如果要在行首查找“option=,最好使用string.compare()而不是find,并将比较的长度限制为optionbuf length。find将在行中的任何位置找到它。整个方法似乎都有缺陷。对于初学者来说,大量的
lineBuf.c_str()
:如果他将结果保存为
char const*
,这是行不通的,如果他将结果保存为
std::string
,这是不必要的。但更一般地说,我会在第一个
'='
处拆分字符串,然后从那里开始。使用某种映射,将密钥映射到要分配的成员地址。如果如果要在行的开头查找“option=,最好使用string.compare()而不是find,并将比较的长度限制为optionbuf length。find将在行中的任何位置找到它。整个方法似乎都有缺陷。对于初学者来说,大量的
lineBuf.c_str()
:如果他将结果保存为
char const*
,这是行不通的,如果他将结果保存为
std::string
,这是不必要的。但更一般地说,我会在第一个
'='
处拆分字符串,然后从那里开始。通过某种映射,将密钥映射到要分配的成员地址。