0

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 のパラメーターは次のとおりです。それらのタイプは long です。

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

しかし、プログラムでtxtを読んだ後、これらを取得します。

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

私は Windows でプログラムをテストしました。

4

1 に答える 1

2

あなたのコードは私にとってはうまくいきます。他の場所にエラーがあるか、私が見つけられなかった未定義の動作が必要です。

まったく同じことを行うためのよりC++の方法を提案できますか:

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また、変数ごとにファイル全体を検索する代わりに、ファイル全体を解析して結果を に保存することもできます。

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;
}
于 2013-10-16T14:34:30.513 に答える