英文:
fopen function is failed to open existing file in read mode (wasm platform)
问题
以下是代码部分的翻译:
#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;
}
代码中涉及的注释部分也已翻译。
英文:
I have compiled the following code using the emcc
compiler
#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;
}
i have tried to compile like,
$emcc mycode.c
$node mycode.js input.txt
Output:
file name: input.txt
File is not opening
there is another way to compile and run like,
$emcc mycode.c --preload-file input.txt -o mycode.html
$node mycode.js input.txt
Output:
file name: input.txt
File is successfully open
but at the time of compilation i have to specify the name of file which is opening in read mode,
is there any way to access file from local file system and opening file without giving --preload-file option
thanks in advance
I am expecting to open file using fopen function in read mode from local file system.
答案1
得分: 0
使用原生文件系统更加困难,需要更多的实验性浏览器功能,但只需传递目录而不是文件,您就可以解决问题。然后,您至少可以选择该目录中的任何文件。
emcc mycode.c --preload-file ./ -o mycode.html
--preload-file
也可以接受目录,尽管名称是如此。
英文:
Using the native file system is harder and requires more experimental browser features, but can you solve the problem by just passing the directory instead of the file? Then you can at least pick any file in that directory.
emcc mycode.c --preload-file ./ -o mycode.html
--preload-file
also takes directories, despite the name.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论