当我运行以下代码时:
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
- 在 list
android.permission.WRITE_EXTERNAL_STORAGE
、android.permission.READ_EXTERNAL_STORAGE
中包含权限 - 测试设备 - 三星 S6
fstream::open()
失败的原因可能是什么?
最佳答案
C文件API模式"w+"
对应iostream open mode 在 |出来 |截断
,而不是in |出
。前者如果不存在则创建一个新文件,后者会导致错误。
关于android - 使用 NDK,std::fstream open() 失败,而 fopen() 正常工作,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40039176/