Help! After 5 hours, and tons of googling, I still cant do find & replace.

I'm an absolute beginner at programming and C++, and while I've made some progress in my project, I've hit upon a brick wall that I just can't seem to get past. After beating my head against this wall for 5 hours now I come to you guys for help.

How do I find an existing string of text in a text file and replace it with a new string of text that the user inputs?

I've been searching google, this forum and others, and have read page after page of basic information on file handling and file input/output, but they all seem to deal with simple input and output scenarios. Nothing I've come across specifically deals with reading in text from a file, finding a piece of text in the middle of said file, replacing that piece of text with user input, the outputting the edited file.

Here's the basic code I have so far that reads in the entire text file. The output is will be an html file. The text in the input file is html markup with placeholders for the specific areas where user input needs to be added.

I just can't figure out how to search the input for the placeholders and replace them.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{

ifstream htmlIn;
ofstream htmlOut;
htmlIn.open("htmlinput.txt");
htmlOut.open("insertindex.html");
string pageContent;

//Check to make sure the file can be opened

if (!htmlIn)
{

cout << "Error opening file.\n";
cout << "Perhaps the file is not where indicated.\n";

return 1;

}//end "if (!htmlIn)"

//Check to see if there is data in the input file

else if ((htmlIn.peek()) == EOF)

cout << "The file is empty" << endl;

else
{

htmlOut << pageContent;

//Check to see if the end of file has been reached
//If not, then read in the input and output it to new file

while (!htmlIn.eof())
{

getline(htmlIn,pageContent);
htmlOut << pageContent;

}//end of while (!htmlIn.eof())

}//end of default else

//The input and output files are now closed

htmlIn.close();
htmlOut.close();

return 0;

}

Any insight or links to specific tutorials dealing with this subject will be a huge help. Thanks.
[2507 byte] By [strain] at [2007-11-19 22:48:30]
# 1 Re: Help! After 5 hours, and tons of googling, I still cant do find & replace.
Are you using Visual Studio?

If you are, all you have to do after having opened the project is click on "Edit", "Find and Replace", then either "Find in Files" or "Replace in Files" as you prefer.

The search results (filename and line number) are presented in a new window (assuming you're doing a Search) and one merely clicks on the line displaying a file name and line number, and that file appears in the code window.

When doing a Replace, the code is replaced automatically - no user intervention required (or permitted).

I MUST have misunderstood your post. Is that all you wanted to do, a replace-in-files? Let me read that post again ...... Oh! maybe you're not using VS 2003, in which case, I can't help ya.

Best wishes,

bill
ThermoSight at 2007-11-10 23:33:00 >
# 2 Re: Help! After 5 hours, and tons of googling, I still cant do find & replace.
Hi strain,

So what you'll want to do when doing a file search and replace is:

1. Create an empty string (we'll call it newFile)

2. Read in the file line by line, checking the line to see if it contains the place holder string (look at the string class's find_first_of method).

3. While it contains that place holder somewhere, replace it with the new string (look at the string class's replace method). Remember, find_first_of returns the starting index of the instance, and replace takes the starting index.

4. Store each changed line into newFile, making sure to add a new line character ("\n") to the end of every changed line, since readline chops them off.

5. Open up your output stream and write out newFile.

Hope that helps. For a good reference on the ANSI string class, look here: http://www.msoe.edu/eecs/cese/resources/stl/string.htm
LooselyBased at 2007-11-10 23:33:55 >
# 3 Re: Help! After 5 hours, and tons of googling, I still cant do find & replace.
1. fill the buffer with the text.

2. check every single byte to see does it match the first byte of searched string.

3. if it does, call a method to check next few bytes, depending of the searched string length, and in case the byte sequence matches searched string, you copy replacement string to the destination buffer.

4. if it does not, copy it to destination buffer.
Cvele at 2007-11-10 23:35:01 >