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

huangapple go评论75阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年3月31日 18:49:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75897671.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定