0

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;
Ken Y-N
  • 12,690
  • 21
  • 62
  • 98
  • 1
    Why don't you concatenate `*i_file` *after* you check whether it's a newline, so you don't have to remove it? – Barmar Feb 13 '18 at 05:37
  • 3
    [`while (i_file != eof)`](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) is the wrong way. Simply use `std::getline()` –  Feb 13 '18 at 05:38
  • 1
    Why do you think a newline in the string is causing `stof()` to fail? The algorithm used by `stof()` simply ignores anything after the number in the string. – Barmar Feb 13 '18 at 05:42
  • 8
    All of this substr stuff seems like a really complicated way to avoid using `>>` to parse your file. – Barmar Feb 13 '18 at 05:43
  • 1
    And if you just want to read until the next slash, you can use `std::getline(Stream, '/');` – Barmar Feb 13 '18 at 05:44
  • 1
    So, what does `mass` actually look like? What happens if you print it out? – Ken Y-N Feb 13 '18 at 05:44
  • 1
    Can you post a sample of your input file format? You should consider using `std::getline(i_file, line, '/')` and even better creating an object to store your input parameters then overload that object's input stream operator with `friend std::istream& operator >>` – Justin Randall Feb 13 '18 at 05:53
  • Print out `mas` and its `size()` to see what's in it: `std::cout << mas.size() << ", " << "'" << mas << "'" << '\n';`. – Galik Feb 13 '18 at 06:51

0 Answers0