|
-
June 17th, 2006, 10:23 PM
#1
Help! After 5 hours, and tons of googling, I still can't 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.
Code:
#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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|