今天应经理要求,写了这么一个类,输入一个HTML文件的地址,输出一个文本文件,记录这个HTML文件里面的src资源的地址
现在想用string类完全改写这个类
先写了一个试用版本
#include <cstdio>
#include <iostream>
#include <cstdlib>
using namespace std;
class FindObject
{
private:
char filename[100];
char line[1025];
FILE *in;
FILE *out;
public:
FindObject();
void GetObject();
};
FindObject::FindObject()
{
cout << "Please input html file's URL:" ;
//cin >> filename;
out = fopen("test.txt","w");
in = fopen("test.html","r");
if (in == NULL)
{
cout << "文件不存在!" << endl;
system("pause") ;
exit(1);
}
}
void FindObject::GetObject()
{
int i, n;
while (fgets(line,1024, in) != NULL)
{
n = strlen(line);
for (i=0; i<n; ++i)
{
if ((line[i+1]=='s' || line[i+1]=='S')
&& (line[i+2]=='r' || line[i+2]=='R')
&& (line[i+3]=='c' || line[i+3]=='c') )
{
char *imgAddress1;
imgAddress1 = strstr(line, "src");
char *imgAddress2;
imgAddress2 = strstr(imgAddress1, "gif");
int length = imgAddress2 - imgAddress1 ; //取得目标字符串的长度.//怀疑这样的减法是不是有问题
char aim[1500];
memset(aim, 0, length);
strncpy(aim, imgAddress1, length);
strcat(aim, "/n"); //加个换行符
cout << aim << endl; //可以选择显示
fputs(aim, out); //写入文本文件
}
}
system("pause");
}
}