I'm trying to make a program that will find a certain word from a text file, then replace that word with something else. Currently, I'm outputting the result in console. My main problem is how to replace a word with another word.
#include <iostream>
#include <string>
#include <fstream>
usingnamespace std;
int main ()
{
string str[1000];
string header;
string content = "This is the content area for the website";
string footer;
string sidebar;
string source[2];
int pos[5];
fstream file;
size_t found;
file.open ("Template.html");
for (int i=0; i<1000; i++)
{
getline(file,str[i]);
}
for (int i = 0; i<1000; i++)
{
found = str[i].find("header");
if (found!=string::npos)
pos[0] = found;
found = str[i].find("content");
if (found!=string::npos)
pos[1] = found;
found = str[i].find("sidebar");
if (found!=string::npos)
pos[2] = found;
found = str[i].find("footer");
if (found!=string::npos)
pos[3] = found;
found = str[i].find("source1");
if (found!=string::npos)
pos[5] = found;
found = str[i].find("source2");
if (found!=string::npos)
pos[6] = found;
}
for (int i = 0; i<1000; i++)
{
str[i].replace(pos[1],5,content); <-- My problem. I randomly entered 5
}
for (int i=0; i<1000; i++)
{
cout<<str[i];
}
return 0;
}