I am new to C++ programming. I want to:
i) open an already created text file in my program
ii) read its content to match some specific string
iii) replace the content with some new string value in that very same file
I have tried to find any solution from google, but failed.
any help would highly be appreciated…
here is an example of what i am intending to do…
#include<iostream>
#include<iomanip>
#include<string>
#include<fstream>
using namespace std;
void main()
{
string name="ABC";
string designation="Student";
int age=19;
fstream outfile;
outfile.open("Data.txt,ios::in|ios::out|ios::app");
outfile<<setw(10)<<name<<setw(10)<<designation<<setw(10)<<age;
outfile>>name;
if(name=="ABC")
{
name="XYZ";
outfile<<name;
}
}
Erm, you need to read the file, make the change, and then write the file. If the replacement you’re making is of a different length, then you need to rewrite the contents of the file after the modification such that they are moved down.