在C++中搜索和编辑文件

Search and Edit a file in C++

本文关键字:编辑文件 搜索 C++      更新时间:2023-10-16

大家美好的一天,我正在为我的C++主题制作一个数据库项目,我想寻求有关如何编辑或替换C++文件的帮助。我找不到可以编辑或替换我创建的文件中的项目的最简单程序。

文本.txt:

name: John Rodriguez
age:12
name: Edward Bantatua
age:15
name: Hemerson Fortunato
age:18

在示例中,我想编辑Hemerson Fortunato并更改他的名字和年龄。谁能帮我做一个程序?,大进步感谢任何帮助我的人。对不起,我的英语不好。

将文件内容读入字符串并使用 replace() .然后将字符串写回文件。像这样:

#include <string>
#include <fstream>
#include <sstream>
using namespace std;
int main()
{
    ostringstream text;
    ifstream in_file("Text.txt");
    text << in_file.rdbuf();
    string str = text.str();
    string str_search = "Fortunato";
    string str_replace = "NotFortunato";
    size_t pos = str.find(str_search);
    str.replace(pos, string(str_search).length(), str_replace);
    in_file.close();
    ofstream out_file("Text.txt");
    out_file << str;     
}

使用regex_replace(C++11)或boost:regex进行更高级的查找和替换操作。

在C++中,文件操作主要有两种不同的方式。
一个是使用Fstream函数,主要用于Turbo C,另一个是FILE作为数据类型。

现在,您可以做的是创建一个文件指针。

fstream fp;
fp.open("Your_file_path.txt","w"); 

上面的代码将帮助您打开文件。接下来,您需要以字符串或字符数组的形式获取此文件。为此,您可以使用 get() 函数。要获得它,您可以添加此

yourarray=fp.get();


在循环直到 (EOF) 中,这意味着文件的结尾也称为 \0。
现在,您已将文件的所有内容复制到 char 数组中。您需要做的就是在数组中搜索您想要的内容,对其进行编辑并用 char 数组替换整个文件内容。

//This is edit mode program
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<iostream.h>
int main()
{
FILE*fp1, *fp2;
char fname[100],lname[100];
char fncomp[100],lncomp[100];
int age; 
fp1 = fopen("OriginalTextFile.txt","r"){
}
fp2 = fopen("TemporaryTextFile.txt","w"){
}
printf("Enter First name of a person that you want to edit: ");
scanf("%s", &fncomp);
while(!feof(fp1)){
fscanf(fp1,"%s %s %d", fname,lname,age);
if(strcmpi(fncomp,fname)==0)
printf("%s %s %dnn", fname,lname,age);
printf("Replace name with: ");
scanf("%s %s",&repfname, &replname);
fname = repfname;
lname = replname;
fprintf(fp2,"%s %s %d", fname,lname,age);
}
fclose(fp1);
fclose(fp2);
fp2 = fopen("TemporaryTextFile.txt","r"){
}
fp1 = fopen("OriginalTextFile.txt","w"){
}
while(!feof(fp2))
{
fscanf(fp2,"%s %s %d", fname,lname,age);
fprintf(fp1,"%s %s %d", fname,lname,age);
}
fclose(fp1);
fclose(fp2);
getch();
}
#include <boostfilesystem.hpp>
#include <boost/regex.hpp>
#include <boost/algorithm/string/replace.hpp>
 std::string str_find = "blablabla";
 std::string str_replace = "nonono";
 std::ifstream filein("C:\Users\myfilein.txt");
 ofstream fileout("C:\Users\myfileout.txt");


 if ( filein )
 {
      std::stringstream buffer;
      buffer << file.rdbuf();
      filein.close();
      // Create a string variable to apply boost::regex
      std::string readText;
      readText = buffer.str();
      // Regular expression finding comments
      boost::regex re_comment(str_find);
      // Replace via regex replace
      std::string result = boost::regex_replace(readText, re_comment, str_replace);
      ofstream out_file(fileout);
      out_file << result;
  }

创建包含任何文本和表达式"blablabla"的文件".txt"。这将替换为"nonono"。

void fileEdit(string filename, string search, string replace)
{
        ostringstream text;
        ifstream in_file(filename);
        text << in_file().rdbuf();
        string str = text.str();
        string str_search = search;
        string str_replace = replace;
        size_t pos = str.find(str_search);
        str.replace(pos, string(str_search).length(), str_replace);
        in_file().close();
        ofstream out_file(filename);
        out_file << str;
    
}

fileEdit("text.txt", "someTextToReplace", "Some more text")

在main()和文件文本中.txt"someTextToReplace"将被替换为"More Text"。