fopen函数在读取模式下打开现有文件失败(wasm平台)



我使用emcc编译器编译了以下代码

#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
if(argc > 2 || argc < 2) {
printf("nEnter the name of file n");
return 0;
}
FILE *file = NULL;
// Open file in read mode
printf("file name: %s",argv[1]);
file = fopen(argv[1], "r");
if(file != NULL) {
printf("nFile is successfully openn");
}
else {
printf("nFile is not openingn");
return 0;
}
fclose(file);
return 0;
}

我已经试过编译了,

$emcc mycode.c
$node mycode.js input.txt

输出:

file name: input.txt
File is not opening 

还有另一种编译和运行的方式,比如

$emcc mycode.c --preload-file input.txt -o mycode.html
$node mycode.js input.txt

输出:

file name: input.txt
File is successfully open

但是在编译时我必须指定以读模式打开的文件名,有没有办法从本地文件系统访问文件并打开文件而不给——preload-file选项

thanks in advance

我期望使用fopen函数从本地文件系统读取模式打开文件。

使用本地文件系统比较困难,需要更多实验性的浏览器特性,但是您可以通过传递目录而不是文件来解决这个问题吗?然后,您至少可以选择该目录下的任何文件。

emcc mycode.c --preload-file ./ -o mycode.html

--preload-file也接受目录,尽管名称。

最新更新