简体   繁体   English

我想删除以%%(C ++)开头的txt文件中的某些行

[英]I want to delete some lines in txt file which starts with %% ( C++)

I have a text file which contains lots of lines like below: 我有一个文本文件,其中包含很多行,如下所示:

%% 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"

I want to delete all lines start with %% and also there are some lines which contains %% in the middle, so delete %% up to the end of that lines too. 我想删除所有以%%开头的行,中间还有一些包含%%的行,因此也要删除%%直到该行的末尾。 I want my final result like this 我想要这样的最终结果

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

Read line by line into a temporary string. 逐行读入一个临时字符串。 Check if first two characters are not equal to "%%" and insert the string into another file: 检查前两个字符是否不等于"%%" ,并将字符串插入另一个文件:

#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 you want to skip the lines that have "%%" at any position modify the above if statement to: 如果要跳过任何位置都有"%%"的行,请将上面的if语句修改为:

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

You don't 'delete' lines, but you create a copy without those lines. 您不会“删除”行,而是创建没有这些行的副本。

For small files, you could do this in memory. 对于小文件,您可以在内存中执行此操作。 However, for large files, you could create a new file, delete the old file, and rename the new file to match the old file. 但是,对于大文件,您可以创建一个新文件,删除旧文件,然后重命名新文件以匹配旧文件。

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

For the deleting/renaming part, look at How to change a text file's name in C++ 对于删除/重新命名部分,请查看如何在C ++中更改文本文件的名称

Do you want to overwrite the old file, or just get the filtered data? 您要覆盖旧文件,还是仅获取过滤后的数据? In case of filtering, I would not use a specialized solution, just a common split functions first result. 在过滤的情况下,我不会使用专门的解决方案,而只会使用常见的拆分功能。

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

output: 输出:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM