2

Good Day to All, I am making a database project for my C++ subject, and I want to ask help about how to edit or replace a file in C++. I cant find a simpliest program that can edit or replace an item in my created file.

Text.txt:

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

In the example I want to edit Hemerson Fortunato and change his name and age. Can anyone help me to make a program for it?, Big Advance Thanks to anyone who help me. Sorry for my bad English.

1

5 Answers 5

6

Read the content of your file into string and use replace(). Then write the string back to the file. Something like this:

#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; } 

Use regex_replace (C++11) or boost:regex for more advanced find & replace operations.

Sign up to request clarification or add additional context in comments.

2 Comments

str.find("Fortunato") is fix am i right? what if something like this: in display: enter word to find: Hemerson Record Found! name: Hemerson Fortunato age: 18 edit file? Y enter new name: Jeremy Renner enter new age: 35 new name and new age save!. i want output like that but i cannot translate it in code. im always good at thinking a logic like that, but when translating sometimes its get harder. please help.
@user2803376, use string name; std:cin >> name; to enter a name from standard input
1

In C++ there are mainly two different ways with file operations.
One is using Fstream functions, that is mainly used in Turbo C and the other one is FILE as a data type.

Now what you can do is create a file pointer.

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

The above code will help you to open ur file. Next you need to get this file in a string or a char array. For that you can use the get() function. To get it, you can add this

yourarray=fp.get(); 


in a loop till (EOF) which means the end of file also called as \0.
Now you have all the contents of ur file copied into a char array. All u need to do is search the array for what u want, edit it and replace the entire file contents with the char array.

Comments

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(); } 

Comments

0
#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; } 

Create your file ".txt" with any text and the expression "blablabla" inside it. This will be replaced by "nonono".

Comments

0
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; } 

Use

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

on main() and in file text.txt "someTextToReplace" will be replaced whit "some more text".

1 Comment

Is there any particular reason for the redefinition of these two variables : string str_search = search; string str_replace = replace; Since they were already given as parameters, is it just about clarity ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.