我有一个jni项目,jni中的文件结构如下:
jni
|--Android.mk
|--Application.mk
|--com_example_jnifile_Filer.h
|--jnifile.cpp
|--res.txt
和jnifile.cpp中的代码如下:
#include "com_example_jnifile_Filer.h"
#include <fstream>
#include <string>
using namespace std;
jstring Java_com_example_jnifile_Filer_n_1get_1string(JNIEnv* env, jclass obj) {
ifstream in("res.txt");
if (!in) {
return env->NewStringUTF("can't open file res.txt!");
}
string s;
getline(in, s);
return env->NewStringUTF(s.c_str());
}
,res.txt中的内容为:
hello world!
所以,问题是,如何通过c ++代码访问文件res.txt? 谢谢你的回答!
答案 0 :(得分:0)
尝试使用以下内容来阅读您的文件:
ifstream in;
in.open ("names.txt");
if(!(in.is_open())){
return env->NewStringUTF("can't open file res.txt!");
}
string s;
getline(in, s);
return env->NewStringUTF(s.c_str());