前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >如何将文件中的一部分段落整体删除

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

作者头像
ljw695
发布2025-02-26 08:44:09
发布2025-02-26 08:44:09
4700
代码可运行
举报
文章被收录于专栏:ljw
运行总次数:0
代码可运行

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

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

Linux

代码语言:javascript
代码运行次数:0
复制
#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 系统:

代码语言:javascript
代码运行次数:0
复制
#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)"),例如:

代码语言:javascript
代码运行次数:0
复制
std::string inputFilePath = R"(C:\Users\YourUsername\Documents\dd.txt)";
std::string outputFilePath = R"(C:\Users\YourUsername\Documents\dd_cleaned.txt)";

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

进行这些操作

dd_cleaned.txt就是改写成的文件

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-02-25,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 执行代码,这里用C++和Linux系统,Windows系统类似
    • Linux
    • Windows
      • 修改说明:
      • 注意事项:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档