samedi 25 novembre 2017

Text parsed line by line with loop operating on groups

In a txt file, populations 0 and 1 each has its own parameter values, formatted like this:

population 0
gK 9
gNa 35
gL 0.1

population 1 
gK 9
gNa 35
gL 0.1

The file is loaded using ifstream, parsed using getline and stringstream.

I try making a loop such that when a line containing "population 0" is encountered, its parameters are read line by line according to the template "contents >> param_name >> param_val", parameter name is compared to an expected string, and its value goes into a corresponding pointer array (ie: population 0, gK=9 goes into gK[0])

Reaching the last expected parameter, or reaching next population (i+1), jumps to seek next population in the text (i+1).

void Parameters::Initialize(ifstream & pfile) {
 stringstream char_content;
 string temp;
 string line;
 string param_name;
 double param_val{ 0 };

 while (getline(pfile, line)) {

    stringstream contents(line);


    if (contents.rdbuf()->in_avail() == 0) { // if 
        continue;
    }

    if (!(contents >> param_name >> param_val)) { 
        printf("error reading line from file. position: file %s line %d\n", __FILE__, __LINE__);
        throw "error reading file";
    }


for (int i { 0 }; i <= 2; ++i) { 

        if ((param_name.compare("Population") == 0) && (param_val == i)) { //if condition satisfied, get subsequent lines up to next population

        if (param_name.compare("gK") == 0); gK[i] = param_val;
        if (param_name.compare("gNa") == 0); gNa[i] = param_val;
        if (param_name.compare("gL") == 0); gL[i] = param_val;

        if ((param_name.compare("Population") == 0) && (param_val == i + 1)) continue;
        }
    }
}
}

int main (){
    cin >> file; // .txt file
    std::ifstream paramFile(file);

    P.Initialize(paramFile);
}

Would appreciate if someone could help make a working version.

Aucun commentaire:

Enregistrer un commentaire