I am writing a program which reads in strings from a file in.txt and then converts it to html friendly text and outputs it to out.txt. When I try to run it I get a segmentation fault, so I am not sure what I am doing wrong.
Also, as you will see in my switch statement I have to convert the " ' " character to "&apos", but I don't know how to write " ' " in c.
Here is my code:
Code:
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<stdbool.h>
int main()
{
FILE *in;
FILE *out = NULL;
in = fopen("in.txt", "r");
if(in == NULL)
{
perror("Unable to open in.txt");
return 1;
}
out = fopen("out.txt", "w");
if(in == NULL)
{
fclose(in);
perror("Unable to open out.txt");
return 1;
}
char line[100];
int i;
while (fgets(&line[i], 100, in) != NULL)
{
for(i=0; i!= '\0' && i<100; ++i)
{
switch (line[i])
{
case '"':
fputs(""", out);
break;
case 'd':
fputs("&apos", out);
break;
case '&':
fputs("&", out);
break;
case '<':
fputs("<", out);
break;
case '>':
fputs(">", out);
break;
default:
fputc(line[i], out);
break;
}
}
}
fclose(out);
fclose(in);
}
Also here is the sample in.txt we are suppose to convert:
This is some text.
<TAG>
"Double Quote Line"
'single quote line'
An ampersand: &
</TAG>
End of file.
Thanks in advance for all the help!