删除html文件中的标签

/*-----------------------------------------------------*/
/*
This program reads an html file, and writes the text
without the tags to a new file.
By Edison Wang            Date:  2009-07-22
*/

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

int main(){
//Declare objects.
char character;
bool text_state(true);
string infile, outfile;
ifstream html;
ofstream htmltext;

//Prompt user for name of out put file.
cout <<"Enter the name of output file: ";
cin >> outfile;

//Prompt user for name of input file.
cout <<"Enter the name of input file: ";
cin >> infile;

//Open files
html.open(infile.c_str());
if (html.fail()) {
cout << "Error opening input file \n";
return 0;
}
htmltext.open(outfile.c_str());

//Read first character form html file.
html.get(character);

while (!html.eof()) {
//check state.
if (text_state) {
if (character=='<') {//beginning of a tag
text_state=false;
}
else{
htmltext << character;  //still text, write to the file.
}
}
else{
//command state, no output required.
if (character=='>') {//end of tag.
text_state = true;   //change states
}
}
//read next character from html file.
html.get(character);
}  //end of while
cout << "transform successfully!";
return 0;
}

posted @ 2009-08-12 05:16  莫忆往西  阅读(152)  评论(0)    收藏  举报