【问题标题】:I want to delete some lines in txt file which starts with %% ( C++)我想删除 txt 文件中以 %% 开头的一些行(C++)
【发布时间】:2017-07-18 08:20:49
【问题描述】:

我有一个包含很多行的文本文件,如下所示:

%% reason of the selling ?
%% they piggy-back on cingular 's service .
zone[-3] %% also , their t-zones ."
customer service[-2] %% since i received the phone.
%% 24 hours ?"
screen[-2] %% i must have heard this about a dozen times over the span"

我想删除所有以 %% 开头的行,并且还有一些中间包含 %% 的行,所以也删除 %% 直到该行的末尾。我想要这样的最终结果

zone[-3]
customer service[-2]
screen[-2]

【问题讨论】:

  • 听起来像是一个 perl 任务。使用 split()。
  • sed 's/%%.*//; /^$/d'

标签: c++


【解决方案1】:

逐行读入一个临时字符串。检查前两个字符是否不等于"%%" 并将字符串插入另一个文件:

#include <iostream>
#include <fstream>
#include <string>
int main(){
    std::ifstream ifs("input.txt");
    std::ofstream ofs("output.txt");
    std::string tempstr;
    while (std::getline(ifs, tempstr)){
        if (tempstr.substr(0, 2) != "%%"){
            ofs << tempstr << std::endl;
        }
    }
}

如果您想跳过任何位置有"%%" 的行,请将上面的if 语句修改为:

if (tempstr.find("%%") == std::string::npos)

【讨论】:

  • 谢谢罗恩,它帮助了我!这真的很有用
  • @mhmdsrjz 真的吗?因为它不能完全满足您的第二个要求:“而且还有一些中间包含 %% 的行,所以也将 %% 删除到该行的末尾。”
  • @Lanting 所以通过查找和替换注释,我找到所有 %% 并用 \n%% 替换它们,现在所有的“%%”都在句子的开头,用什么罗恩告诉我,我删除了多余的行!这种方式帮助我;)
【解决方案2】:

您不会“删除”行,而是创建一个没有这些行的副本。

对于小文件,您可以在内存中执行此操作。但是,对于大文件,您可以创建一个新文件,删除旧文件,然后重命名新文件以匹配旧文件。

ifstream inFile;
ofstream outFile;
string line;
while (getline(inFile, line)) // reads the line
  {
    //remove everything after %%
    size_t percentIdx = line.find("%%");
    string lineWithoutComment = line.substr(0, percentIdx);
    //if the line is not empty, send everything before that to the outFile
    if (! lineWithoutComment.empty())
      {
        outFile << lineWithoutComment << endl;
      }
  }

删除/重命名部分看How to change a text file's name in C++

【讨论】:

    【解决方案3】:

    您是要覆盖旧文件,还是只获取过滤后的数据? 在过滤的情况下,我不会使用专门的解决方案,只是一个常见的拆分功能第一个结果。

    #include <string>
    
    //surely you have your own splitting function, this is just an example
    std::vector<std::string> stringSplit(std::string s, std::string delim, int start/*=0*/)
    {
        std::vector<std::string> result;
        std::string s1 = s + delim; //to find at least one
        auto delimPos = s1.find(delim, start), 
            delimLen = delim.length(),
            pMax = s1.length(), tokenLen = delimPos - start,
            startPos = delimPos - tokenLen;
        while (((int)delimPos > -1) && (delimPos < pMax) )
        {
            tokenLen = delimPos - startPos;
            result.push_back(s1.substr(startPos, tokenLen));
            startPos = delimPos + delimLen;
            delimPos = s1.find(delim, startPos);
        }
        return(result);
    }
    
    
    std::vector<std::string> lines = {
            "%% reason of the selling ?",
            "zone[-3] %% also , their t-zones .",
            "customer service[-2] ## since i received the phone.",
            "customer service[-2] %% since i received the phone.",
            "%% 24 hours ?\"",
            "screen[-2] %% i must have heard this about a dozen times over the span\"" };
    std::string result;
    for (auto line : lines)
    {
        result = (stringSplit(line, "%%"))[0];
        if (result != "")
            std::cout << result << std::endl;
    }
    

    输出:

    zone[-3]
    customer service[-2] ## since i received the phone.
    customer service[-2]
    screen[-2]
    

    【讨论】:

      猜你喜欢
      • 2019-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-31
      • 1970-01-01
      • 1970-01-01
      • 2015-12-02
      • 2023-02-12
      相关资源
      最近更新 更多