Skip to content

Instantly share code, notes, and snippets.

@harieamjari
Last active July 29, 2022 11:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harieamjari/111fd2399970aac9d2dcc57c6f28ede6 to your computer and use it in GitHub Desktop.
Save harieamjari/111fd2399970aac9d2dcc57c6f28ede6 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
#include <assert.h>
int main(int argc, char *argv[]){
assert(argc == 3);
char *file_name = argv[1];
char *html_title = argv[2];
FILE *fpin = stdin;
FILE *fpout = fopen(file_name, "wb");
assert(fpout != NULL);
fprintf(fpout,
"<!DOCTYPE html>\n"
"<html>\n"
"\t<head>\n"
"\t\t<title>%s</title>\n"
"\t</head>\n"
"\t<body>\n", html_title);
char buf[1024] = {0};
int new_par = 1;
while (fgets(buf, 1024, fpin) != NULL){
if (new_par)
fprintf(fpout, "\t\t<p>\n");
int len = strlen(buf);
/* remove carriage return on DOS text files */
if (len > 2 && buf[len - 3] == '\r')
memcpy(buf + (len - 3), (char[]){'\n', 0}, 2);
if (buf[0] == '\n'){
fprintf(fpout, "\t\t</p>\n");
new_par = 1;
continue;
}
fprintf(fpout, "%s", buf);
new_par = 0;
}
fprintf(fpout, "\t</body>\n</html>\n");
fclose(fpout);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment