从文件中删除注释并保留整数 [英] Remove comments from file and keep integers

查看:41
本文介绍了从文件中删除注释并保留整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的 .txt 文件中删除评论.我的文本文件如下所示:

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

注释是 (* 和 *) 之间的所有内容.我只需要保留这个文件中的 58、63、70 和 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] != ')') {
            file.erase(file.begin(),file.begin()+i);
        }
    }
    }
    copy(begin(file), end(file), ostream_iterator<char>(cout, " "));
    }

我不应该使用 vector.erase() 吗?我知道这段代码是不对的.如果是这种情况,更好的解决方案是什么?我知道在 C 中您可以将其写入内存并转到每个位置,这是更好的方法吗?

Should I not use vector.erase()? 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?

推荐答案

我会首先将所有内容保存为一个字符串,准备该字符串,然后然后安全地将结果推回到一个向量中.现在我使用 std::regex 来过滤你的文件.不过,这不是最简单的.

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
    //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;
}

输出:

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

当然,使用 std::stoi 仅在整数之后没有字符时才有效.嗯,这只是一个想法,当然可以高度修改.

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.

这篇关于从文件中删除注释并保留整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆