C++中使用正则表达式删除一行文本

在 C++ 中,您可以使用正则表达式删除一行文本,方法是逐行读取文件,将正则表达式模式应用于每一行,然后排除与模式匹配的行。以下是如何使用 C++ 中的正则表达式删除一行文本的示例:

#include <iostream>
#include <fstream>
#include <regex>

int main() {
    std::ifstream file("input.txt");
    std::ofstream outfile("output.txt");
    std::regex pattern("pattern_to_match");

    if (!file.is_open() || !outfile.is_open()) {
        std::cout << "Error opening file." << std::endl;
        return 1;
    }

    std::string line;
    while (std::getline(file, line)) {
        if (!std::regex_search(line, pattern)) {
            outfile << line << std::endl;
        }
    }

    file.close();
    outfile.close();

    std::cout << "Line deleted successfully." << std::endl;

    return 0;
}

在上面的代码中,程序逐行读取名为 "input.txt "的文件,并将不匹配 regex 模式 "pattern_to_match "的行写入名为 "output.txt "的新文件。 您可以将 "pattern_to_match "替换为您想用来删除文件中的行的 regex 模式。 确保在代码中包含必要的头文件并正确处理文件输入/输出错误。

如何用 C 语言从文本文件中读取特定行?

#include <stdio.h>

int main()
{
    int line_number = 3; // the line number you want to read
    FILE *file = fopen("example.txt", "r"); // open the file in read mode
    char line[256]; // buffer to store the line
    int current_line = 1; // the current line number
    while (fgets(line, sizeof(line), file)) {
        if (current_line == line_number) {
            printf("Line %d: %s", line_number, line);
            break;
        }
        current_line++;
    }
    fclose(file);
    return 0;
}

这段代码将读取文件 "example.txt",并打印文件的第 3 行。 要读取的行号存储在变量 "line_number "中,可以通过修改该变量来读取不同的行。

要用 C 语言从文本文件中读取特定行,可以使用以下步骤:

  1. 使用 fopen() 函数打开文件。
  2. 使用循环使用 fgets() 函数逐行读取文件。
  3. 使用计数器变量来计数行号。
  4. 当达到所需的行号时,将该行存储在缓冲区中。
  5. 使用 fclose() 函数关闭文件。
以下是从名为“example.txt”的文件读取第三行的示例代码片段:

#include <stdio.h> 

int main() { 
    FILE *file; 
    char line[100]; 
    int lineNum = 1; 

    file = fopen("example.txt", "r"); 
    if (file) { 
        while (fgets(line, sizeof(line), file)) { 
            if (lineNum == 3) { 
                printf("第三行: %s", line); 
            } 
            lineNum++; 
        } 
        fclose(file); 
    } 
    return 0; 
}

在此示例中,使用 fopen() 函数以读取模式打开文件“example.txt”。在循环中使用 fgets() 函数逐行读取文件。循环使用 lineNum 变量跟踪行号。当到达第三行时,该行存储在缓冲区“line”中并使用 printf() 打印。最后,使用 fclose() 函数关闭文件。

如何从 php 中的文本文件中删除一行?

<? php

$path =  'test.txt' ;
 // 要删除的行
$deleteLine =  “测试” ;

// 创建临时文件
$temp =  tempnam ( './' , 'tmp' );
 // 以读取模式打开文件
$resource =  fopen ($path, "r" );
 if ($resource) {
     // 读取文件
    while(!feof($resource)){
        $buffer =  fgets ($resource, 4096 );
         // 检查是否不需要删除此行
       if (trim($buffer)!== $deleteLine){
             file_put_contents($temp,$buffer,FILE_APPEND);
        }
    }
    fclose ($resource);
     // 删除文件
    unlink ($path);
     // 重命名临时文件
    rename($temp,$path);
}

要从 PHP 中的文本文件中删除一行,可以使用以下步骤:

  1. 以写入模式使用 fopen() 函数打开文本文件。
  2. 使用 fgets() 函数逐行读取文件内容。
  3. 检查每一行,看它是否与要删除的行匹配。
  4. 如果不匹配,则使用 fwrite() 函数将该行写入临时文件。
  5. 处理完文件中的所有行后,关闭输入和输出文件句柄。
  6. 使用 unlink() 函数删除原始文件。
  7. 使用 rename() 函数将临时文件重命名为原始文件名。

$filename =  "myfile.txt" ; 
$temppath =  "temp.txt" ; 
$line_to_delete =  "要删除的行" ; 

$fp =  fopen ($filename, "r" ); 
$tp =  fopen ($temppath, "w" ); if ($fp && $tp) {
     while ( ! feof ($fp)) { 
        $line = fgets ($fp);
         if ($line !== $line_to_delete) {
             fwrite ($tp, $line); 
        } 
    } fclose ($fp);
     fclose ($tp);
     unlink ($filename);
     rename ($temppath, $filename); 
}

在本例中,我们假设要修改的文件名为 "myfile.txt",并且要删除包含字符串 "要删除的行 "的行。 代码以读取模式打开输入文件,以写入模式打开临时输出文件。 然后使用 fgets() 读取输入文件的每一行,并使用 fwrite() 将其写入临时文件,但与要删除的行匹配的行除外。 最后,它会关闭文件句柄,删除原始文件,并将临时文件重命名为原始文件名。