c++ - Remove comments from file and keep integers -


i trying remove comments .txt file. text file looks this:

(* sunspot data collected robin mcquinn *) (* http://sidc.oma.be/html/sunspot.html         *)  (* month: 1749 01 *) 58 (* month: 1749 02 *) 63 (* month: 1749 03 *) 70 (* month: 1749 04 *) 56 

the comments between (* , *).i need keep 58,63,70, , 56 file.

my code removing of chars not properly. code looks this:

#include <iostream> #include <vector> #include <iterator> #include <algorithm> #include <fstream> #include <string> #include <cctype> #include <numeric> #include <iomanip>  using namespace std;  int main() {      int digit = 1;     string filename;     //cout getting user path     //the compiler parses string literals differently use double backslash or forward slash     cout << "enter path of data file, sure include extension." << endl;     cout << "you can use either of following:" << endl;     cout << "a forwardslash or double backslash separate each directory." << endl;     getline(cin, filename);      //gets file     ifstream infile{filename};     istream_iterator<char> infile_begin{ infile };     istream_iterator<char> eof{};     vector<char> file{ infile_begin, eof };      for(int =0; < file.size(); i++){     if(!isdigit(file[i])) {         if(file[i] != ')') {             file.erase(file.begin(),file.begin()+i);         }     }     }     copy(begin(file), end(file), ostream_iterator<char>(cout, " "));     } 

should not use vector.erase()? know not right in code. if case better solution? know in c can write memory , go each location, better way?

i first save string, prepare string , then safely push_back result vector. used std::regex filter file. it's not easiest, though.

#include <iostream> #include <string> #include <regex> #include <fstream>  int main(){      std::string file_name;     std::cout << "enter name/path of txt file: ";     std::getline(std::cin, file_name);     std::ifstream file(file_name);      std::vector<int> vec; //here save integers      std::string text; //save current line here       std::smatch match; //here found "comment" get's saved, later removed text      std::regex remove("[\(\*]\.*[\*\)] *"); //the expression search     //translation     //     _[\(\*]   -> (*     //     _\.*      -> number of characters     //     _[\*\)]   -> *)     //     _ *       -> number of whitespaces (important cast integer)..        while (std::getline(file, text)){ //loop through lines in file.txt          if (std::regex_search(text, match, remove)){ //if comment found             text.erase(text.begin(), text.begin() + match[0].length()); //remove comment         }          if (!text.empty()) { //empty, line pure comment             vec.push_back(std::stoi(text)); //else add integer list         }     }       std::cout << "the file contains:" << std::endl;     (int = 0; < vec.size(); i++){         std::cout << vec.at(i) << std::endl;     }      return 0; } 

ouput:

enter name/path of txt file: file.txt file contains: 58 63 70 56 

of course, using std::stoi works if there no characters after integer. well, idea , of course highly modifiable.


Comments

Popular posts from this blog

serialization - Convert Any type in scala to Array[Byte] and back -

matplotlib support failed in PyCharm on OSX -

python - Matplotlib: TypeError: 'AxesSubplot' object is not callable -