简体   繁体   中英

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? I think passing jstring in is correct but I don't know how to convert to pull path name for fopen() .

Try using this function which converts jstring to 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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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