How do I read a user-specified file in an emscripten compiled library?

前端 未结 4 921
北荒
北荒 2021-01-11 19:07

I\'m currently working on a file parsing library in C with emscripten compile support. It takes a file path from the user where it reads the binary file and parses it.

4条回答
  •  半阙折子戏
    2021-01-11 19:44

    If you want compile this file directly into library you can use --preload-file or --embed-file option. Like this:

    emcc main.cpp -o main.html --preload-file /tmp/my@/home/caiiiycuk/test.file
    

    After that in C you can open this file normally:

    fopen("/home/caiiiycuk/test.file", "rb")
    

    Or you can use emscripten javascript fs-api, for example with ajax:

    $.ajax({
        url: "/dataurl",
        type: 'GET',
        beforeSend: function (xhr) {
            xhr.overrideMimeType("text/plain; charset=x-user-defined");
        },
        success: function( data ) {
            Module['FS_createDataFile']("/tmp", "test.file", data, true, true);
        }
    });
    

    After that you can open this file from C. Also it is not best way to pass data into C code, you can pass data directly in memory, read about this.

提交回复
热议问题