简体   繁体   中英

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 (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++

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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