一段关于从HTML文件找出资源地址的小程序(不断更新)

今天应经理要求,写了这么一个类,输入一个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");
    }
}  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值