Delete comments from file and save integers

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 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] != ')') {
            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?

+4
source share
3 answers

, , . std:: regex . , .

#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 saved, later to be removed from text

    std::regex remove("[\(\*]\.*[\*\)] *"); //the expression to search for
    //translation
    //     _[\(\*]   -> (*
    //     _\.*      -> 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 << vec.at(i) << std::endl;
    }

    return 0;
}

Ouput:

Enter name/path of the txt file: file.txt
The file contains:
58
63
70
56

, std::stoi , . , , , .

+4

, , . , , ).

, , , , (* end *) ?

std::vector<std::string> fileContent;
std::string line;
while (std::getline(infile, line))
{
    //Find starting character sequence
    auto begin = line.find("(*");
    if (begin != std::string::npos)
    {
        //Find matching ending sequence, it not a comment otherwise
        auto end = line.find("*)", begin);
        if (end != std::string::npos)
            line.erase(line.begin() + begin, line.begin() + end + 2);
    }

    fileContent.push_back(line);
}
+2

You can use std :: getline to read after the closing character ')', then you know that the next read will be your number:

int main()
{
    std::ifstream ifs("test.txt");

    std::string line;
    while(std::getline(ifs, line)) // line by line
    {
        std::string skip;
        int value;

        // skip data upto and past ')', then read number
        if(std::getline(std::istringstream(line), skip, ')') >> value)
            std::cout << "found: " << value << '\n';
    }
}

Conclusion:

found: 58
found: 63
found: 70
found: 56
0
source

Source: https://habr.com/ru/post/1653929/


All Articles