您好,我有一个编写程序的任务,该程序可以从HTML代码中打印出某些单词。例如,我们有以下HTML代码:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <body>
        <div>
            <p>2014</p>
<span> wasap </span>
</div>
<div>
<p> 2013 </p>
<span> hahahaha labas </span>
</div>
<div>
<p> 2012 </p>
<span> blalbalba </span>
</div>
</body>
</html>


因此输出应如下所示:wasap haha​​haha labas blablabla

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LENGHT 2048

struct htmlCode {
    char subjectName[20];
    char c[MAX_LENGHT - 1];
} htmlCode;

int main() {

    int i = 0, size = 0;
    struct htmlCode dataSubject[100];
    char span1[6];
    char span2[7];
    FILE *fp = fopen("duomenys.txt", "r");

    while (!feof(fp)) {
        fscanf(fp, "%s", &htmlCode.c);
        size++;
        //printf("%d %s\n", size, c);
    }

    //printf("%d\n", size);
    rewind(fp);
    if (fp) {
        while (!feof(fp)) {
            for (i = 0; i < size; i++) {
                fscanf(fp, "%s", &htmlCode.c);
                strcpy(dataSubject[i].c, htmlCode.c);
                //printf("%d %s\n", i, dataSubject[i].c);
                if (feof(fp))
                        break;
            }
        }
    }
    printf("%d\n", size);

    strcpy(span1, "<span>");
    printf("%s\n", span1);
    strcpy(span2, "</span>");
    printf("%s\n", span2);
    rewind(fp);
    for (i = 0; i < size; i++) {
        if (dataSubject[i].c == span1) {
            printf("%s\n", dataSubject[i].c);
        }
    }

    fclose(fp);

    return 0;
}


有人可以帮我吗?

最佳答案

您快拥有了!在问题代码中只需考虑几件事:



问题代码出现以下两行:

fscanf(fp, "%s", &htmlCode.c);


都更改为:

fscanf(fp, "%s", htmlCode.c);




更改:

   if (dataSubject[i].c == span1) {
      printf("%s\n", dataSubject[i].c);
      }


至:

   if(0 == strcmp(dataSubject[i].c, span1))
      {
      ++i;

      while(i < size)
         {
         if(0==strcmp(dataSubject[i].c, span2))
            break;

         printf("%s ", dataSubject[i].c);
         ++i;
         }
      }


请参阅完整代码here

10-08 03:58