#include #include #include #include #include using namespace std; string title; vector coordinates; vector colors; vector stepTime; vector >> data; // first dim = step; second dim = point; third dim = coordinate ( or component ) double stringToDouble( const std::string& s ) { std::istringstream i(s); double x; if (!(i >> x)) return 0; return x; } bool check_double( const std::string& s ) { std::istringstream i(s); double x; if (!(i >> x)) return false; return true; } bool stripeInfo(string &line, string tag) { auto titlePos = line.find(tag); auto titleFound = false; if (titlePos != std::string::npos) { //cout << "TAG " << tag << " FOUND!!!!" << endl; titleFound = false; auto const pos = line.find_last_of(':'); line = line.substr(pos+1); return true; } return false; } std::string ReplaceAll(std::string str, const std::string& from, const std::string& to) { size_t start_pos = 0; while((start_pos = str.find(from, start_pos)) != std::string::npos) { str.replace(start_pos, from.length(), to); start_pos += to.length(); // Handles case where 'to' is a substring of 'from' } return str; } bool isEmpty(std::string str) { if ( ReplaceAll(str, " ", "").empty() ) return true; else return false; } void extractInfo(string line) { cout << "check comment info in: " << line << endl; auto titleFound = stripeInfo(line, "Title"); auto coordinatesFound = stripeInfo(line, "Coordinates"); auto colorsFound = stripeInfo(line, "Colors"); auto stepTimeFound = stripeInfo(line, "Time"); istringstream iss(line); if ( titleFound || coordinatesFound || colorsFound || stepTimeFound ) { do { string word; iss >> word; auto i = 0; if ( !word.empty() ) { if ( titleFound ) { cout << "Title: " << word << endl; title += word; } if ( coordinatesFound ) { cout << "Coordinate[" << coordinates.size() << "]: " << word << endl; coordinates.push_back(word); } if ( colorsFound ) { cout << "Colors[" << colors.size() << "]: " << word << endl; colors.push_back(word); } if ( stepTimeFound ) { cout << "Time: " << word << endl; stepTime.push_back(stringToDouble(word)); } } } while (iss); } } void t2(void) { ifstream file; file.open ("testc.dat"); string line; line.clear(); auto commentFound = false; auto emptyLineFound = false; cout << "-----------------------" << endl; // Read file line by line while (!file.eof()) { getline(file, line); // Check if it is a comment line auto commentPos = line.find("#"); if ( commentPos == 0 ) { //cout << "This is a comment. " << endl; commentFound = true; // Check if it has any information tag extractInfo(line); } else { //cout << "This is NOT a comment. " << endl; commentFound = false; } // Check if the line is empty (will start new group of data,e.i.a new "step") if ( isEmpty(line) ) { if ( !emptyLineFound ) { data.push_back(vector >); // Create a "step" in data multi dim vector emptyLineFound = true; } cout << "EMPTY LINE" << endl; } // Is line is not empty and not a comment so it contains data (coordinates) else if ( !commentFound ) { emptyLineFound = false; // Split line into words istringstream iss(line); data[data.size() -1].push_back(vector); do { string word; iss >> word; if ( !word.empty()) { cout << "Word: " << word; if ( check_double(word) ) cout << " : is a double" << endl; data[data.size() -1].push_back(vector); else cout << " : is NOT a double" << endl; } } while (iss); } cout << "-----------------------" << endl; } } void show(void) { cout << "==============================" << endl; cout << "Title = " << title << endl; for (auto i = 0; i < coordinates.size(); i++) cout << "Coordinate[" << i << "] = " << coordinates[i] << endl; for (auto i = 0; i < stepTime.size(); i++) cout << "Time[" << i << "] = " << stepTime[i] << endl; for (auto i = 0; i < colors.size(); i++) cout << "Colors[" << i << "] = " << colors[i] << endl; cout << "==============================" << endl; } void test-vector1(void) { vector< vector > vec; for (int i = 0; i < 10; i++) { vector row; // Create an empty row for (int j = 0; j < 20; j++) { row.push_back(i * j); // Add an element (column) to the row } vec.push_back(row); // Add the row to the main vector } } void test-vector2(void) { vector< vector > vec; for (int i = 0; i < 10; i++) { vec.push_back(vector()); // Add an empty row } for (int j = 0; j < 20; j++) { for (int i = 0; i < vec.size(); i++) { vec[i].push_back(i * j); // Add column to all rows } // You could also use iterators: for (vector< vector >::iterator it = vec.begin(); it != vec.end(); ++it) { it->push_back(j); // Add column to all rows } } } int main (void) { t2(); show(); }