I have this code. The purpose of the code is to print everything before <img> and everything after </img>. Everything in between <img> and </img> should not be printed. However, I am having 2 issues.
- The code is not compiling on windows (gcc compiler)?
- The code is printing everything. It is even printing the
<img>,</img>and everything in between.
Code:
FILE* fp = fopen("test.html", "r");
char* line;
size_t len;
ssize_t read;
bool found_tag = false;
int line_storer;
char* before_tag;
char* after_tag;
while ((read = getline(&line, &len, fp)) != -1) {
if (
((before_tag = strstr(line, "<img>")) != NULL) &&
((after_tag = strstr(line, "</img>")) != NULL)
) {
line_storer = before_tag - line;
printf("%.*s", line_storer, line);
printf("The Image use to be here\n");
line_storer = after_tag - line + strlen("</img>");
printf("%s", line + line_storer);
} else if ((before_tag = strstr(line, "<img>")) != NULL) {
line_storer = before_tag - line;
printf("%.*s", line_storer, line);
found_tag = true;
} else if((after_tag = strstr(line, "</img>")) != NULL) {
found_tag = false;
line_storer = after_tag - line + strlen("</img>");
printf("%s", line + line_storer);
} else if(!found_tag) {
printf("%s", line);
}
}
fclose(fp);
test.html:
<b>This is a test page</b>
<div class=back1>Some more text here for more testing!!!!</div>
<img>www.website.com/image.png</img>
The Image use to be here
<i>More words</i>
<u><i><b>TESTING 123</u></i></b>
output:
<b>This is a test page</b>
<div class=back1></div>
The Image use to be here
<i>More words</i>
<u><i><b>TESTING 123</u></i></b>
Assumptions:
There will only be one <img>
There will only be one </img>. The </img> tag will always be after the <img>
Aucun commentaire:
Enregistrer un commentaire