简体   繁体   English

转换jstring和追加的语法

[英]Syntax for converting jstring and appending

JNIEXPORT int JNICALL Java_com_ndkfoo_test_GL2JNILib_step2(JNIEnv * env, jobject obj,  jstring filePath)
{
    const jbyte *str;
    str = (*env)->GetStringUTFChars(env, filePath, NULL);
    char* fullPath=str.append("FileName.txt"); // error
    char* fullPath2=str+"fileName.txt"          // error
}

Could someone please indicate the right syntax to create define fullPathName? 有人可以指出正确的语法来创建定义fullPathName吗? I think passing jstring in is correct but I don't know how to convert to pull path name for fopen() . 我认为传递jstring是正确的但我不知道如何转换为fopen()拉取路径名。

Try using this function which converts jstring to std:string: 尝试使用此函数将jstring转换为std:string:

void GetJStringContent(JNIEnv *AEnv, jstring AStr, std::string &ARes) {
  if (!AStr) {
    ARes.clear();
    return;
  }

  const char *s = AEnv->GetStringUTFChars(AStr,NULL);
  ARes=s;
  AEnv->ReleaseStringUTFChars(AStr,s);
}

Solution of your task: 解决您的任务:

JNIEXPORT int JNICALL Java_com_ndkfoo_test_GL2JNILib_step2(JNIEnv * env, jobject obj,  jstring filePath) {
    std::string str;
    GetJStringContent(env,filePath,str);
    const char *fullPath = str.append("FileName.txt").c_str();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM