【问题标题】:Ifstream won't open file C++ with JNA Android StudioIfstream 不会使用 JNA Android Studio 打开文件 C++
【发布时间】:2018-04-08 19:33:54
【问题描述】:

我正在尝试使用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

【问题讨论】:

  • 您收到任何错误吗?如果是这样,请将其包含在您的帖子中。
  • 不,我没有收到错误消息或任何东西。
  • 您能否验证文件是否存在于给定位置?通常程序将 bin 文件夹作为当前位置
  • 是的,我也这样做了。我打开了一个 Xcode 项目并运行了完全相同的代码,它能够找到该文件。

标签: android c++ fstream jna


【解决方案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 库或类似的东西。无论如何它都有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多