使用JNA和Android Studio打开C++文件时出现ifstream无法打开文件的问题

3
我将尝试使用fstream读取文件。我正在使用C++11编写代码,并通过Android Studio的JNI与Java进行交互。但是,由于某种原因,它无法打开文件。我使用的是相对文件路径,但不明白为什么无法打开文件。该文件名为proverbs.txt。文件名中没有任何差异,例如proverbs.txt.txt等。

以下是代码:

void storeProverbs() {
    string path = "/Users/tenealaspencer/Desktop/proverbs.txt";

    std::ifstream provInput(path.c_str(), std::ios::in);

    //provInput.open("/Users/tenealaspencer/Desktop/proverbs.txt");

    // opens the proverbs text file
    equivInput.open("/Users/tenealaspencer/AndroidStudioProjects/example/app/src/main/cpp/stored.txt"); // opens the stored (English) proverbs text file

    if (!provInput.is_open()) {
        cout << "error ";
    }

    while (!provInput.eof()) // while not at the end of the proverbs file

    {
        getline(provInput, phrase); // read proverbs in line by line
        getline(equivInput, storedProv); // read english proverbs in line by line

你收到任何错误信息了吗?如果有,请在您的帖子中包含它。 - Onur A.
没有,我没有收到任何错误信息或其他提示。 - Yng Kody
你能验证给定位置是否存在文件吗?通常程序将bin文件夹作为当前位置。 - Onur A.
是的,我也这样做了。我打开了一个 Xcode 项目并运行了完全相同的代码,它能够找到文件。 - Yng Kody
1个回答

1

没关系,我刚刚使用以下 Java 代码导入了文件:

try {
    InputStream is = getAssets().open("stopwords.txt");

    String line1;

    BufferedReader reader = new BufferedReader(new InputStreamReader(is));


    while((line1 = reader.readLine()) != null) //
    {
        try {
            byte[] utf8Bytes = line1.getBytes("UTF8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        storeStopWords(line1);
    }
}
catch (IOException e) {
    e.printStackTrace();
}


try  {
    InputStream is = getAssets().open("proverbs.txt");
    InputStream iz = getAssets().open("stored.txt");

    String line;
    String line2; //= new String ("");

    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    BufferedReader reader2 = new BufferedReader(new InputStreamReader(iz));

    while((line = reader.readLine()) != null && (line2 = reader2.readLine()) != null ) //
    {
        try {
            byte[] utf8Bytes = line.getBytes("UTF8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        storeProverbs(line,line2);
    }
}
catch (IOException e) {
    e.printStackTrace();
}

我在某个地方读到JNA不支持fstream库或类似的东西。无论如何,它可以工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接