使用NDK, std::fstream open()失败,而fopen()正常工作,为什么?

With NDK, std::fstream open() is failing, whereas fopen() works correctly, Why?

本文关键字:常工作 为什么 fopen 工作 open fstream NDK 使用 失败 std      更新时间:2023-10-16

当我运行以下代码时:

char const * path = "/path/to/file";
std::fstream fs;
fs.open(path, fstream::in | fstream::out | fstream::binary);
if (!fsWave) {
    LOGE("open, Error opening file %s", path);
}

打印错误日志open, Error opening file /path/to/file

,以下工作顺利:

FILE * pf = NULL;
if(NULL == (pf = fopen(path, "w+b"))) {
    LOGE("open, Error opening file %s", path);
}

平滑地,我的意思是,它不打印错误日志,在指定位置创建文件。设置>

    Android Studio 2.2.1
  • NDK 12.1.2977051
  • 清单android.permission.WRITE_EXTERNAL_STORAGE, android.permission.READ_EXTERNAL_STORAGE中包含的权限
  • 测试设备-三星S6

fstream::open()失败的原因是什么?

C文件API模式"w+"对应于iostream打开模式in | out | trunc,而不是in | out。如果该文件不存在,前者将创建一个新文件,后者将导致错误。