使用 NDK,std::fstream open() 失败,而 fopen() 正常工作,为什么?
With NDK, std::fstream open() is failing, whereas fopen() works correctly, Why?
当我运行以下代码时:
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 open mode in | out | trunc
,而不是 in | out
。前者如果文件不存在则创建一个新文件,后者会导致错误。
当我运行以下代码时:
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 open mode in | out | trunc
,而不是 in | out
。前者如果文件不存在则创建一个新文件,后者会导致错误。