【问题标题】:With NDK, std::fstream open() is failing, whereas fopen() works correctly, Why?使用 NDK,std::fstream open() 失败,而 fopen() 正常工作,为什么?
【发布时间】:2016-10-14 09:04:40
【问题描述】:

当我运行以下代码时:

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
  • 测试设备 - 三星 S6

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

【问题讨论】:

  • 一般in/out confusion? fopen 调用的语义与您的 fstream 版本不同。

标签: android c++ android-ndk fstream


【解决方案1】:

C文件API模式"w+"对应iostreamopen modein | out | trunc,而不是in | out。前者不存在则新建文件,后者则报错。

【讨论】:

  • 谢谢。 open mode 链接真的很有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多