如何将文件中的一部分段落整体删除

目录

执行代码,这里用C++和Linux系统,Windows系统类似

Linux

Windows

修改说明:

注意事项:

假设下图这是一个10万多字的文章,有很多③部分的内容,我们想要将它的段落全部删除,但是在word和pdf修改器中都没法删除,就可以运用代码帮助了

执行代码,这里用C++和Linux系统,Windows系统类似

Linux

#include <iostream>
#include <fstream>
#include <string>

void removeParagraphAfterMarker(const std::string& inputFilePath, const std::string& outputFilePath, const std::string& marker) {
    std::ifstream inputFile(inputFilePath);
    std::ofstream outputFile(outputFilePath);

    if (!inputFile.is_open() || !outputFile.is_open()) {
        std::cerr << "Error opening file!" << std::endl;
        return;
    }

    std::string line;
    bool skipParagraph = false;

    // 逐行读取文件
    while (std::getline(inputFile, line)) {
        if (line.find(marker) != std::string::npos) {
            skipParagraph = true; // 标记段落开始
            continue;
        }

        if (skipParagraph) {
            // 如果当前段落应该被跳过
            if (line.empty()) {
                skipParagraph = false; // 下一段不跳过
            }
            continue;
        }

        // 写入有效段落到输出文件
        outputFile << line << std::endl;
    }

    inputFile.close();
    outputFile.close();
}

int main() {
    std::string inputFilePath = "/home/ljw/删除③/dd.txt"; // 输入文件路径
    std::string outputFilePath = "/home/ljw/删除③/dd_cleaned.txt"; // 输出文件路径
    std::string marker = "③"; // 要删除的段落标记

    removeParagraphAfterMarker(inputFilePath, outputFilePath, marker);

    std::cout << "Processing complete. Output saved to " << outputFilePath << std::endl;

    return 0;
}

Windows

将这段代码转换为适用于 Windows 系统的版本,主要需要调整文件路径的格式。在 Windows 系统中,路径通常使用反斜杠(\),而不是 Linux 系统中的正斜杠(/)。此外,由于反斜杠在 C++ 中是转义字符,因此需要使用双反斜杠(\\)来表示路径分隔符。

以下是修改后的代码,适用于 Windows 系统:

#include <iostream>
#include <fstream>
#include <string>

void removeParagraphAfterMarker(const std::string& inputFilePath, const std::string& outputFilePath, const std::string& marker) {
    std::ifstream inputFile(inputFilePath);
    std::ofstream outputFile(outputFilePath);

    if (!inputFile.is_open() || !outputFile.is_open()) {
        std::cerr << "Error opening file!" << std::endl;
        return;
    }

    std::string line;
    bool skipParagraph = false;

    // 逐行读取文件
    while (std::getline(inputFile, line)) {
        if (line.find(marker) != std::string::npos) {
            skipParagraph = true; // 标记段落开始
            continue;
        }

        if (skipParagraph) {
            // 如果当前段落应该被跳过
            if (line.empty()) {
                skipParagraph = false; // 下一段不跳过
            }
            continue;
        }

        // 写入有效段落到输出文件
        outputFile << line << std::endl;
    }

    inputFile.close();
    outputFile.close();
}

int main() {
    std::string inputFilePath = "C:\\Users\\YourUsername\\Documents\\dd.txt"; // 输入文件路径
    std::string outputFilePath = "C:\\Users\\YourUsername\\Documents\\dd_cleaned.txt"; // 输出文件路径
    std::string marker = "③"; // 要删除的段落标记

    removeParagraphAfterMarker(inputFilePath, outputFilePath, marker);

    std::cout << "Processing complete. Output saved to " << outputFilePath << std::endl;

    return 0;
}

修改说明:

  1. 文件路径

    • 将文件路径中的正斜杠(/)替换为双反斜杠(\\)。

    • 示例路径修改为 C:\\Users\\YourUsername\\Documents\\dd.txtC:\\Users\\YourUsername\\Documents\\dd_cleaned.txt,请根据实际情况替换 YourUsername 和文件路径。

  2. 其他部分

    • 代码逻辑未做改动,因为文件操作和字符串处理在 Windows 和 Linux 系统中是相同的。

注意事项:

  • 确保输入文件路径和输出文件路径是正确的,并且程序有权限访问这些路径。

  • 如果文件路径较长或包含特殊字符,建议使用原始字符串字面量(R"(path)"),例如:

    std::string inputFilePath = R"(C:\Users\YourUsername\Documents\dd.txt)";
    std::string outputFilePath = R"(C:\Users\YourUsername\Documents\dd_cleaned.txt)";

    这样可以避免手动处理双反斜杠。

进行这些操作

dd_cleaned.txt就是改写成的文件

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值