-3

Hello I have a task to write a program, that prints out certain words from HTML code. For example we have this HTML code:

<?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>

So the output should look like this: wasap hahahaha 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;
}

Can anyone help me with that please?

Nikas Žalias
  • 1,594
  • 1
  • 23
  • 51

1 Answers1

1

You almost have it! Just few small things to consider in the question code:


The question code has two occurrences of the following line:

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

Change both to:

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

Change:

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

To:

   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;
         }
      }

See the full code here .

Mahonri Moriancumer
  • 5,993
  • 2
  • 18
  • 28
  • 1
    -1 the full code is still wrong with that eof-check. – The Paramagnetic Croissant May 30 '14 at 15:32
  • @user3477950, you are correct that the full code has a lot of things left to clean-up. However, it compiles without warnings or errors, and it produces the expected result. The question breaks most of my rules for writing maintainable code: http://www.mahonri.info/doc/MahonriListOfRulesForWritingMaintainableCCode.html However, the question asked was for some 'help', not for a full-blown __Mahonri'ized__ solution. Hence, the full code is not meant to be perfect; rather just an example of what I did to get the question code working. – Mahonri Moriancumer May 30 '14 at 15:38