I am trying to remove comments from my .txt file. My text file looks like this:
(* Sunspot data collected by Robin McQuinn from *)
(* http://sidc.oma.be/html/sunspot.html *)
(* Month: 1749 01 *) 58
(* Month: 1749 02 *) 63
(* Month: 1749 03 *) 70
(* Month: 1749 04 *) 56
Comments are all between (* and *). I need to save only 58.63.70 and 56 from this file.
My code removes some characters, but not correctly. My code is as follows:
#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 << "Enter the path of the data file, be sure to include extension." << endl;
cout << "You can use either of the following:" << endl;
cout << "A forwardslash or double backslash to separate each directory." << endl;
getline(cin, filename);
ifstream infile{filename};
istream_iterator<char> infile_begin{ infile };
istream_iterator<char> eof{};
vector<char> file{ infile_begin, eof };
for(int i =0; i < 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 be used vector.erase()? I know this is wrong in this code. If so, what is the best solution? I know that in C you can write it to memory and go to every place, would it be better?
source
share