我已经使用 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 open\n");
}
else {
printf("\nFile is not opening\n");
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 选项
提前致谢
我希望在本地文件系统的读取模式下使用 fopen 函数打开文件。