C Coding, gcc compiler HTML(Hypertext Markup Language) is the primary document d
ID: 3779228 • Letter: C
Question
C Coding, gcc compiler
HTML(Hypertext Markup Language) is the primary document description language used in webpages. Certain characters are not rendered in browsers as they are special characters used in HTML (in partcular tags which begin and end with the characters. To display such characters correctly they need to be escaped (similar to how you need to escape tabs and endline characters in C). Properly escaping these characters is not only important for proper rendering, but there are also security issues involved (Cross-site Scripting Attacks). You will write a program to open an HTML file, scrub certain characters and write the results to a new output file. While there are many characters that can/should be converted to escaped-HTML characters, your function will only need to support a few. In particular, make sure that your function replaces the characters in Table 1. Place your source code in a file named htmlScrubber.c; it accepts input and output file names from the command line. That is, it should be callable via: ~^>./a.out CSEUNLHomePage.html scrubbedCSEUNLIiomePage.html The program should open the first file, convert all special characters as listed above and output the result to the second file. The contents of the first file should not be altered.Explanation / Answer
SOURCE CODE:
#include <stdio.h>
int main( int argc, char *argv[] ) {
FILE *fp1,*fp2;
fp1 = fopen(argv[1], "r");
fp2 = fopen(argv[2], "w+");
int ch=getc(fp1);
while(ch != EOF)
{
if(ch=='&')
{
putc('&',fp2);
putc('a',fp2);
putc('m',fp2);
putc('p',fp2);
putc(';',fp2);
}
else if(ch=='<')
{
putc('&',fp2);
putc('l',fp2);
putc('t',fp2);
putc(';',fp2);
}
else if(ch=='>')
{
putc('&',fp2);
putc('g',fp2);
putc('t',fp2);
putc(';',fp2);
}
else if(ch=='"')
{
putc('&',fp2);
putc('q',fp2);
putc('u',fp2);
putc('t',fp2);
putc(';',fp2);
}
else
{
putc(ch,fp2);
}
ch=getc(fp1);
}
fclose(fp1);
fclose(fp2);
return 1;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.