1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
void html2js(char file_name[]) {
FILE *html, *js;
char buf[1024];
size_t i, len;
char tmp[30];
strcpy(tmp, file_name);
html = fopen(strcat(tmp, ".html"), "r");
js = fopen(strcat(file_name, ".js"), "w");
if (html == NULL || js == NULL) {
printf("ERROR:Fail to open file!\n");
exit(1);
}
while (!feof(html)) {
memset(buf, '\0', 1024);
fgets(buf, 1024, html);
len = strlen(buf);
fprintf(js, "document.writeln(\"");
for (i = 0; i < len - 1; i++) {
if (buf[i] == '\"' || buf[i] == '\'') {
fprintf(js, "\\");
}
fprintf(js, "%c", buf[i]);
}
if (buf[i] != '\n') {
fprintf(js, "%c", buf[i]);
}
fprintf(js, "\");\n");
}
fclose(html);
fclose(js);
printf("Success!\n");
}
int main() {
char file_name[30];
strcpy(file_name, "common/script4code");
html2js(file_name);
strcpy(file_name, "common/script4works");
html2js(file_name);
return 0;
}
|