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

时间:2016-10-14 09:04:40

标签: android c++ android-ndk fstream

当我运行以下代码时:

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_STORAGEandroid.permission.READ_EXTERNAL_STORAGE
  • 中包含的权限
  • 测试设备 - Samsung S6

可能是什么原因,fstream::open()失败了?

1 个答案:

答案 0 :(得分:5)

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