gpt4 book ai didi

c++ - 读取配置文件

转载 作者:行者123 更新时间:2023-11-28 00:53:26 25 4
gpt4 key购买 nike

我正在尝试读取这样的配置文件:

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;
}

但它不起作用。我的整数是 0 或一些大数字。我的字符串仍然是空的。

有人可以帮帮我吗?

亲切的问候,

最佳答案

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

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

关于c++ - 读取配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12778968/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com