当前位置: 首页 > news >正文

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

目录

执行代码,这里用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就是改写成的文件

相关文章:

  • Grafana使用日志5--如何重置Grafana密码
  • 基于 sklearn 的均值偏移聚类算法的应用
  • [前端] 学习内容总结,css样式居中以及点击包裹a标签的容器元素也能触发a标签的点击事件
  • PyTorch 环境中 CUDA 版本冲突问题排查与解决
  • Linux相关知识(文件系统、目录树、权限管理)和Shell相关知识(字符串、数组)
  • Android 常用命令和工具解析之存储相关
  • 潜水泵,高效排水,守护城市与农田|深圳鼎跃
  • 最快安装ESP8266 ESP832 开发板·Arduino环境的方法
  • Android OpenGLES2.0开发(十一):渲染YUV
  • kafka数据拉取和发送
  • MYSQL之相关子查询
  • Jmeter接口并发测试
  • 图像处理、数据挖掘、数据呈现
  • JavaScript数组
  • 结构型模式 - 适配器模式 (Adapter Pattern)
  • 图像处理案例06 OCR应用
  • C++基础入门——Vetor与函数
  • 华为机试牛客刷题之HJ11 数字颠倒
  • KylinSP3 | 防火墙和麒麟安全增强设置KySec
  • 【电机控制器】ESP32-C3语言模型——通义千问
  • 个人网站如何加入百度联盟/百度营稍
  • 在线建设房屋设计网站/磁力宝
  • 上海松江做网站建设/新冠咳嗽怎么办
  • 泰州网站建设报价/iis搭建网站
  • 企业自己做网站方法/廊坊关键词优化平台
  • 菏泽网站建设招聘/seo搜索引擎专员