Emscripten (1) Generating HTML
1. 產生 HTML:
編輯一個 hello.c
#include <stdio.h>
int main() {
printf("Hello, world!\n");
return 0;
}
編譯並輸出成 HTML。
$ emcc hello.c -o hello.html
用 browser 開啟 HTML。
2. 讀取檔案:
編輯 hello.txt。
Hello world Hello alice Hello bob
編輯 fio.c。
#include <stdio.h>
int main() {
FILE *file = fopen("hello.txt", "rb");
if (!file) {
printf("cannot open file\n");
return 1;
}
while (!feof(file)) {
char c = fgetc(file);
if (c != EOF) {
putchar(c);
}
}
fclose (file);
return 0;
}
file 要用 preload 的方式編進去。
$ emcc fio.c -o fio.html --preload-file hello.txt
JavaScript is usually run in the sandboxed environment of a web browser, without direct access to the local file system.
Emscripten simulates a file system that you can access from your compiled C/C++ code using the normal libc stdio API.
留言
張貼留言