I am trying to delete a newline character at the end of line read from a file. The program compiles no problem but it crashes only when I have the stof function actually called and not commented out. I am assuming it has something to do with the string containing a newline character. I tried to delete the newline but I can't seem to get it right.
int array_count = 0;
Element e1;
ifstream Stream(filename); // Periodic Table Object
ofstream setup("test.txt");
ofstream output("Molecule_list.txt");
string file_contents; //string to store buffer line
istreambuf_iterator<char> i_file(Stream); //open filestream buffer iterator
//string line = Acc_element(fname);
istreambuf_iterator<char> eof; //open end of file streambuffer
while (i_file != eof)
{
string slash = "/";
file_contents += *i_file;
if (*i_file == '\n')
{
array_count = array_count + 1;
/////////////Create Element List
size_t found1 = file_contents.find(slash); //placement of slash
size_t found2 = file_contents.find(slash, (found1)+1); //placement of slash 2
size_t found3 = file_contents.find(slash, (found2)+1); //placement of slash 3
size_t found4 = file_contents.find(slash, (found3)+1); //placement of slash 4
size_t found5 = file_contents.find(slash, (found4)+1); //placement of slash 5
size_t found6 = file_contents.find(slash, (found5)+1); //placement of slash 6
string name = file_contents.substr(0, found1); //create string for the name
string formula = file_contents.substr(found1 + 1, (found2-found1)-1);
string iupac = file_contents.substr(found2 + 1, (found3-found2)-1);
string m_class = file_contents.substr(found3 + 1, (found4 - found3)-1);
string m_sub_class = file_contents.substr(found4 + 1, (found5 - found4)-1);
size_t endcount = (found5 - found4) - 1;
string mass = file_contents.substr(found5 + 1, (found6 - found5) - 4);
/////THE stof function is what creates the error
size_t endlpos;
if ((endlpos = mass.find('\n')) != string::npos)
mass.erase(endlpos);
stof(mass);
// i think the endline causes the error float mass_f = stof(mass);
//cout << array_count<<endl;
setup << "Molecule " << formula << ";" << endl; // Create Element line
/* Needed for the actual reading of files to add chemicals.
Element_array[array_count].E_mass = stof(mass);
Element_array[array_count].E_name = name;
Element_array[array_count].E_formula = formula;
*/
output << name << "." << "M_name = " << "\"" << name << "\"" << ";" << endl; //create the Name line
output << name << "." << "M_formula =" << "\"" << formula << "\"" << ";" << endl;
output << name << "." << "M_iupac = " << iupac << endl;
output << name << "." << "M_class = " << m_class << endl;
output << name << "." << "M_sub_class = " << m_sub_class << endl;
cout.setf(ios::fixed);
output << name << "." << "M_mass = " << mass;
file_contents.clear();
}
i_file++;
}
return;