如何读取emscripten编译库中的用户指定文件? [英] How do I read a user-specified file in an emscripten compiled library?

查看:263
本文介绍了如何读取emscripten编译库中的用户指定文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用emscripten编译支持C语言中的文件解析库。它从用户读取二进制文件并解析它的文件路径。

据我所知,emscripten不支持直接加载文件,而是使用虚拟文件系统。有没有办法将文件加载到虚拟文件系统的指定路径,以便emscripten编译的C库可以读取它?我正在寻找NodeJS和浏览器的解决方案。

解决方案

如果你想将这个文件直接编译到库中,你可以使用--preload-file或--embed-file选项。像这样:

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

之后在C中可以正常打开这个文件:

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

或者您可以使用emscripten javascript fs-api ,例如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);
}
});

之后,您可以从C.
打开这个文件。将数据传递到C代码,您可以直接在内存中传递数据,阅读有关此


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.

I understand that emscripten doesn't support direct loading of files, but instead uses a virtual filesystem. Is there any way to load the file at the given path into the virtual filesystem so that the emscripten compiled C lib can read it? I'm looking for solutions for both NodeJS and in the browser.

解决方案

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.

这篇关于如何读取emscripten编译库中的用户指定文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆