Joe Preney
New member
The purpose of this program is to read in the contents of a file called "in.txt" and convert it to a file called "out.html"
Steps in Detail:
1.#include stdio.h, stdlib.h, ctype.h, and stdbool.h.
2.Declare a FILE* variable called in to fopen a file called in.txt for read access.
3.If in is NULL then the file could not be opened. Call perrror with "Unable to open in.txt" and return 1;.
4.Declare a FILE* variable called out to fopen a file called out.html for write access.
5.If out is NULL then the file could not be opened. Call fclose to close in, call perrror with "Unable to open out.html" and return 1;.
6.NOTE: You'll must read in the file line-by-line, at most 99 characters at a time (i.e., 100 characters are required since the '{rss:Content}' character has to be at the end of the file.
7.Declare a string with 100 elements called line.
8.While fgets() is not equal to NULL:
Write a for loop using an integer counter variable to index line. The loop must terminate when you encounter the '{rss:Content}' character in line.
Write a switch statement to handle the following cases for the current character in line as indexed by the for loop counter variable:
": Output " using fputs to out.
': Output ' using fputs to out.
&: Output & using fputs to out.
<: Output < using fputs to out.
>: Output > using fputs to out.
Default case: Output the character in line at the current offset using fputc to out.
Close the for loop.
Close the while loop.
9.Call fclose(out) to close the input file.
10.Call fclose(in) to close the input file.
Code so far:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdbool.h>
int main()
{
FILE *in;
in=fopen("in.txt","r");
if(in==NULL)
{
perror("Unable to open in.txt");
return 1;
}
FILE out;
out=fopen("out.html", "w");
if(out==NULL)
{
fclose(in);
perror("Unable to open out.html");
return 1;
}
charline[100];
while(fgets(line, 100, in)!=NULL)
{
for(inti=0;i!='{rss:Content}';i++)
{
switch(line)
{
case '"':
fputs("""", out);
break;
case ''':
fputs("'", out);
break;
case '&':
fputs("&", out);
break;
case '<':
fputs("<", out);
break;
case '>':
fputs(">", out);
break;
default:
fputc(line, out);
break;
}
}
}
fclose(out);
fclose(in);
return 0;
}
Steps in Detail:
1.#include stdio.h, stdlib.h, ctype.h, and stdbool.h.
2.Declare a FILE* variable called in to fopen a file called in.txt for read access.
3.If in is NULL then the file could not be opened. Call perrror with "Unable to open in.txt" and return 1;.
4.Declare a FILE* variable called out to fopen a file called out.html for write access.
5.If out is NULL then the file could not be opened. Call fclose to close in, call perrror with "Unable to open out.html" and return 1;.
6.NOTE: You'll must read in the file line-by-line, at most 99 characters at a time (i.e., 100 characters are required since the '{rss:Content}' character has to be at the end of the file.
7.Declare a string with 100 elements called line.
8.While fgets() is not equal to NULL:
Write a for loop using an integer counter variable to index line. The loop must terminate when you encounter the '{rss:Content}' character in line.
Write a switch statement to handle the following cases for the current character in line as indexed by the for loop counter variable:
": Output " using fputs to out.
': Output ' using fputs to out.
&: Output & using fputs to out.
<: Output < using fputs to out.
>: Output > using fputs to out.
Default case: Output the character in line at the current offset using fputc to out.
Close the for loop.
Close the while loop.
9.Call fclose(out) to close the input file.
10.Call fclose(in) to close the input file.
Code so far:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdbool.h>
int main()
{
FILE *in;
in=fopen("in.txt","r");
if(in==NULL)
{
perror("Unable to open in.txt");
return 1;
}
FILE out;
out=fopen("out.html", "w");
if(out==NULL)
{
fclose(in);
perror("Unable to open out.html");
return 1;
}
charline[100];
while(fgets(line, 100, in)!=NULL)
{
for(inti=0;i!='{rss:Content}';i++)
{
switch(line)
{
case '"':
fputs("""", out);
break;
case ''':
fputs("'", out);
break;
case '&':
fputs("&", out);
break;
case '<':
fputs("<", out);
break;
case '>':
fputs(">", out);
break;
default:
fputc(line, out);
break;
}
}
}
fclose(out);
fclose(in);
return 0;
}