在C++; P> GooDaytoDealPosivices,我为我的C++主题做了一个数据库项目,我想问一下如何在C++中编辑或替换文件。我找不到一个最简单的程序可以编辑或替换我创建的文件中的项目

在C++; P> GooDaytoDealPosivices,我为我的C++主题做了一个数据库项目,我想问一下如何在C++中编辑或替换文件。我找不到一个最简单的程序可以编辑或替换我创建的文件中的项目,c++,C++,Text.txt: name: John Rodriguez age:12 name: Edward Bantatua age:15 name: Hemerson Fortunato age:18 在本例中,我想编辑Hemerson Fortunato并更改他的姓名和年龄。有人能帮我做一个程序吗?非常感谢所有帮助我的人。对不起,我的英语不好。将文件内容读入字符串并使用replace()。然后将字符串写回文件。大概是这样的: #include <string> #inclu

Text.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;     
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
ostringstream文本;
_文件中的ifstream(“Text.txt”);

C++中的文本

主要有两种不同的文件操作方式。 现在您可以做的是创建一个文件指针

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

yourarray=fp.get();

在循环中直到(EOF),这意味着文件的结尾也称为\0。
现在,您已经将文件的所有内容复制到了一个字符数组中。您只需在该数组中搜索所需内容,对其进行编辑,然后用字符数组替换整个文件内容。

//这是编辑模式程序
//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 %d\n\n", 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();
}
#包括 #包括 #包括 #包括 int main() { 文件*fp1,*fp2; 字符fname[100],lname[100]; 字符fncomp[100],lncomp[100]; 智力年龄; fp1=fopen(“OriginalTextFile.txt”、“r”){ } fp2=fopen(“TemporaryTextFile.txt”,“w”){ } printf(“输入要编辑的人的名字:”); scanf(“%s”和&fncomp); 而(!feof(fp1)){ fscanf(fp1,“%s%s%d”,fname,lname,age); 如果(strcmpi(fncomp,fname)==0) printf(“%s%s%d\n\n”,fname,lname,age); printf(“将名称替换为:”); 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”){ } 而(!feof(fp2)) { fscanf(fp2,“%s%s%d”,fname,lname,age); fprintf(fp1,“%s%s%d”,fname,lname,age); } fclose(fp1); fclose(fp2); getch(); }
#包括
#包括
#包括
std::string str_find=“blabla”;
std::string str_replace=“nonono”;
std::ifstream filein(“C:\\Users\\myfilein.txt”);
流文件输出(“C:\\Users\\myfileout.txt”);
如果(文件输入)
{
std::stringstream缓冲区;
缓冲器

在main()和text.txt文件中,“someTextToReplace”将替换为“some more text”。

从它开始显示使用streams/files.str.find(“fortunao”)的标准库方法我说的对吗?如果像这样:在显示中:输入word以查找:Hemerson记录已找到!名称:Hemerson Fortunato年龄:18编辑文件?Y输入新名称:Jeremy Renner输入新年龄:35新名称和新年龄保存!。我想要这样的输出,但我无法在代码中翻译它。我总是善于思考这样的逻辑,但当翻译有时会变得困难。请帮助。@user2803376,使用
字符串名称;std:cin>>名称;
从标准输入输入输入名称
#include <boost\filesystem.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;
  }
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")