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 でプログラムをテストしました。