user5468794 user5468794 - 1 year ago 68
C++ Question

Remove comments from file and keep integers

I am trying to remove comments from my .txt file. My text file looks like this:

(* Sunspot data collected by Robin McQuinn from *)
(* *)

(* Month: 1749 01 *) 58
(* Month: 1749 02 *) 63
(* Month: 1749 03 *) 70
(* Month: 1749 04 *) 56

The comments are everything between (* and *).I need to only keep the 58,63,70, and 56 from this file.

My code is removing some of the chars but not properly. My code looks like 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 for getting user path
//the compiler parses string literals differently so use a double backslash or a forward slash
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);

//gets file
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] != ')') {
copy(begin(file), end(file), ostream_iterator<char>(cout, " "));

Should I not use
? I know that it is not right in this code. If that is the case what is the better solution? I know in C you can write it to memory and go to each location, would this be the better way?

Answer Source

I would first save everything as a string, prepare the string and then safely push_back the result into a vector. Now I used std::regex to filter your file. It's not the easiest, though.

#include <iostream>
#include <string>
#include <regex>
#include <fstream>

int main(){

    std::string file_name;
    std::cout << "Enter name/path of the 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 the found "comment" get's saved, later to be removed from text

    std::regex remove("[\(\*]\.*[\*\)] *"); //the expression to search for
    //     _[\(\*]   -> (*
    //     _\.*      -> any number of characters
    //     _[\*\)]   -> *)
    //     _ *       -> any number of whitespaces (important to cast to integer)..

    while (std::getline(file, text)){ //loop through all lines in file.txt

        if (std::regex_search(text, match, remove)){ //if a comment was found
            text.erase(text.begin(), text.begin() + match[0].length()); //remove the comment

        if (!text.empty()) { //empty, line was a pure comment
            vec.push_back(std::stoi(text)); //else add integer to list

    std::cout << "The file contains:" << std::endl;
    for (int i = 0; i < vec.size(); i++){
        std::cout << << std::endl;

    return 0;


Enter name/path of the txt file: file.txt
The file contains:

of course, using std::stoi works only if there are no characters after the integer. Well, this is just an idea and of course highly modifiable.