从字符串中打印出某些单词

时间:2014-05-30 14:43:37

标签: c

你好我有一个编写程序的任务,它打印出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;
}

有人可以帮我吗?

1 个答案:

答案 0 :(得分:1)

你几乎拥有它!问题代码中只需要考虑几件小事:


问题代码出现以下两行:

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