0

I have some input that looks like this:

CH4
9_site_nonpolar
66
#TEMP #PRES #V #DENSITY
115.0 00.1 10186012.74173 0.00017
145.0 00.1 12709964.99224 0.00014
175.0 00.1 15482213.03440 0.00011
205.0 00.1 18140114.97385 0.00010
...

The number 66 refers to the number of data lines (i.e. those under the #TEMP... line.)

I am trying to take this input, ignore the first two lines, store the third line, ignore the fourth line, and then parse the rest of the lines into a vector of strings. The code below is my attempt, but it segfaults when I attempt to access members of the vector this_line.

I am relatively new to C++ and have been having a difficult time debugging this. I think the problem is with the copy() lines (which I got here;) the two cout statements print to stdout and then the program fails.

The vector all_runs in the code has type vector<vector<run>> where a run is just a struct I am using to hold these variables. The outer vector represents each of the files the program takes as input, while the inner vector is intended to hold information from the data lines (e.g. 115.0 00.1 10186012.74173 0.00017.)

    string file_name,
           line;
    for(int i = 1;i < argc;i++)
    {
        int j = 0;
        file_name = argv[i];
        ifstream input(file_name);
        input.ignore();
        input.ignore();
        int num_runs = atof(line.c_str());
        input.ignore();
        if(input.is_open())
        {
            while(getline(input,line))
            {
                vector<string> this_line;
                istringstream iss(line);
                //copy the numbers of interest from the line into the vector this_line
                cout << "hello"<< endl;
                copy(
                        istream_iterator<string>(iss),
                        istream_iterator<string>(),
                        back_inserter(this_line)
                );
                cout << "hi again" << endl;
                auto &ref = (all_runs[i-1])[j];//make current run a ref to clean up the code a bit
                ref.temperature = atof(this_line[0].c_str());
                ref.pressure_atm = atof(this_line[1].c_str());
                ref.simulation_V = atof(this_line[2].c_str());
                ref.density = atof(this_line[3].c_str());
                if(j == (num_runs - 1))
                    break;
                else
                    j++;
            }
        }
        input.close();
    }

Thank you for your time and any help you may be able to offer!

Luciano
  • 493
  • 5
  • 14
  • The right tool to solve such problems is your debugger. You should step through your code line-by-line *before* asking on Stack Overflow. For more help, please read [How to debug small programs (by Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). At a minimum, you should [edit] your question to include a [Minimal, Complete, and Verifiable](http://stackoverflow.com/help/mcve) example that reproduces your problem, along with the observations you made in the debugger. – πάντα ῥεῖ Jun 10 '17 at 19:12
  • Where do you adjust the size of `all_runs`? (If you don't, that's a problem, and possibly *the* problem.) – molbdnilo Jun 10 '17 at 19:19
  • I would change the second `cout` to display `this_line.size()`. The code following that assumes there are at least 4 elements. – Blastfurnace Jun 10 '17 at 19:41

0 Answers0