C++ 在Linux中从txt读取参数

C++ 在Linux中从txt读取参数,c++,linux,C++,Linux,我需要从我的Linux程序的txt文件中读取参数。但结果是,从txt文件读取的一些参数的值是正确的,但其中一些参数的值是错误的。有人遇到了这个问题?我已经用命令dos2unix将windows中的txt格式翻译成Linux。我需要你的帮助,谢谢 读取功能如下所示: template <class T>int ReadFileVar(ifstream *inClientFile, const char var_name[], T *var) { //inClientFile - poi

我需要从我的Linux程序的txt文件中读取参数。但结果是,从txt文件读取的一些参数的值是正确的,但其中一些参数的值是错误的。有人遇到了这个问题?我已经用命令dos2unix将windows中的txt格式翻译成Linux。我需要你的帮助,谢谢

读取功能如下所示:

template <class T>int ReadFileVar(ifstream *inClientFile, const char var_name[], T *var)
{
//inClientFile - pointer to the previously opened File stream
//var_name - contains the name of the variable
//var - pointer to a long, the function will return the value of the variable in this

int length_var_name = (int) strlen(var_name);
char line[512];
int i, j;

while (inClientFile->getline(line,512))
{
    if (line[0] != '/' && line[1] != '/')
    {
        i = 0;
        while (line[i] != '\0')
        {
            if (!strncmp(&line[i],var_name,length_var_name))
            {
                j = i + length_var_name;
                while (line[j] != '\0')
                {
                    if ( line[j] >= '0' && line[j] <= '9')
                    {
                        *var = (T) atof(&line[j]);
                        inClientFile->seekg( 0, ios_base::beg ); //back to the beginning of the file
                        return 0;
                    }
                    j++;
                }
            }
            i++;
        }
    }
}

cerr << var_name << " - cannot be found" << endl;
throw "error reading input data from: ";

return 1; //the specified variable was not found in the file
}
但是在我的程序中读了txt之后我得到了这些

 nx=100;
 ny=100;
 nz=15;
 ipro=1;
 jpro=1;
 kpro=100;

我已经在Windows下测试了这个程序,它可以工作了

你的代码对我有效,你肯定在其他地方有错误,或者是我没有发现的未定义的行为

我可以建议一个C++的方法来做同样的事情:< /P>
template <class T>
T ReadFileVar(ifstream& inClientFile, string var_name)
{
    string line;
    while (getline(inClientFile, line))
    {
        if (line[0] != '/' && line[1] != '/')
        {
            size_t pos = line.find(var_name);
            if( pos != string::npos) {
                pos = line.find('=', pos + 1);
                if(pos == string::npos) {
                    throw std::exception();
                }
                istringstream iss(line.substr(pos + 1));
                T result;
                iss >> result;
                inClientFile.seekg( 0, ios_base::beg );
                return result;
            }
        }
    }
    throw std::exception();
}

为什么要在每个参数之后返回到文件的开头?如果参数类型很长,为什么要使用
atof
?啊,我看到你为每个参数调用该函数。不过,这似乎是一种非常复杂的参数读取方式。为什么不简单地逐行扫描文件一次,并将参数名称和值存储在一个合适的结构中,例如a
map
?我认为该函数没有错误,因为我在Windows中测试过它,它工作得很好。但在Linux中,它有一些错误,正如我所说的。我在txt文件中有很多参数,它们可能是long、int、char等等。@nyarlathotep@Michael
template <class T>
T ReadFileVar(ifstream& inClientFile, string var_name)
{
    string line;
    while (getline(inClientFile, line))
    {
        if (line[0] != '/' && line[1] != '/')
        {
            size_t pos = line.find(var_name);
            if( pos != string::npos) {
                pos = line.find('=', pos + 1);
                if(pos == string::npos) {
                    throw std::exception();
                }
                istringstream iss(line.substr(pos + 1));
                T result;
                iss >> result;
                inClientFile.seekg( 0, ios_base::beg );
                return result;
            }
        }
    }
    throw std::exception();
}
map<string, string> ParseFile(ifstream& inClientFile) {
    map<string, string> result;
    string line;
    while (getline(inClientFile, line))
    {
        if (line[0] != '/' && line[1] != '/')
        {
            size_t pos = line.find('=');
            if(pos == string::npos) {
                throw std::exception();
            }
            string var_name = line.substr(0, pos);
            string var_value = line.substr(pos + 1);
            result[var_name] = var_value;
        }
    }
    return result;
}

template <class T>
T ReadVar(map<string, string> data, string var_name)
{
    map<string, string>::iterator it = data.find(var_name);
    if(it == data.end()) {
        throw exception();
    }
    string value = it->second;
    istringstream iss(value);
    T result;
    iss >> result;
    return result;
}