Adam said:
Hi,
Does anyone know of an online C-to-HTML converter to display code
nicely formatted in a browser?
Thanks a lot,
Adam
Try this:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int status = 0;
int ch;
FILE *in, *out;
in = argc < 2 ? stdin : fopen(argv[1], "r");
if (in == NULL) {
fprintf(stderr, "Cannot open '%s' ", argv[2]);
perror("for input");
return EXIT_FAILURE;
}
out = argc < 3 ? stdout : fopen(argv[2], "w");
if (out == NULL) {
fprintf(stderr, "Cannot create or open '%s' ", argv[2]);
perror("for output");
fclose(in);
return EXIT_FAILURE;
}
fputs("<html><head></head><body><p><pre>", out);
do switch (ch = getc(in)) {
case '"': fputs(""", out); continue;
case '&': fputs("&", out); continue;
case '<': fputs("<", out); continue;
case '>': fputs(">", out); continue;
default : putc(ch, out); continue;
case EOF: if (ferror(in)) perror("Read error"); break;
} while (ch != EOF);
fputs("</pre></p></body></html>\n", out);
status = ferror(in) || ferror(out) ? EXIT_FAILURE : 0;
if (fclose(in) != 0) {
perror("Error closing input file");
status = EXIT_FAILURE;
}
if (fclose(out) != 0) {
perror("Error closing output file");
status = EXIT_FAILURE;
}
return 0;
}
HTH.