Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // open the file for importing data
- ifstream myfile("test_data.txt");
- if (myfile.is_open())
- {
- // eof allows all the rows of data to be converted one at a time
- while (!myfile.eof())
- {
- getline (myfile, bLine); // grab the immediate line as a string
- // this next while loop converts the row at hand into individual strings, which are converted to type double.
- while (bLine.find(",", 0) != string::npos) // loop until there isn't a comma
- {
- int comma_pos = bLine.find(",", 0); // look for the comma position
- double value = atof(bLine.substr(0, comma_pos).c_str());
- temp.push_back(value);
- bLine = bLine.substr(comma_pos + 1, bLine.size() - comma_pos - 1); // remove the substring before the first comma
- }
- // the final value, which doesn't have a comma after it, hasn't been extracted
- temp.push_back(atof(bLine.c_str()));
- // push back temporary 1D row vector into the 2D vector
- data.push_back(temp);
- // add one to the rowcount
- rowcount++;
- // the above section will loop for each row - pushing each row's numbers to the data vector, and counting the rows
- }
- // close the file after reading from it
- cout << "There are " << rowcount << " rows in the CSV file.\n";
- myfile.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement