JNI: Reading a text file in C code and returning to sdk

后端 未结 2 2024
野趣味
野趣味 2021-01-16 06:14

Im trying to create an android app to read text from a text file using NDK .My C code reads a string into a variable and returns the string variable to java code .But when I

相关标签:
2条回答
  • Try this:

    JNIEXPORT jstring JNICALL Java_com_example_openfile_MainActivity_ndkopenfile
    (JNIEnv *env, jobject this)
    {
            char myStr[20];
            FILE* fp = fopen("/sdcard/x.txt","w+");
            if(fp!=NULL)
            {
                fgets(myStr,20,fp);
                fflush(fp);
                fclose(fp);
                return(*env)->NewStringUTF(env,myStr);
            }
            else
            {
                fclose(fp);
                return(*env)->NewStringUTF(env,"Error opening file!");
            }
    }
    

    This technique works perfectly !!! Tested.

    0 讨论(0)
  • 2021-01-16 07:01

    I think your definition jstring str1[20]; need to modify to char str1[20]. Because jstring type is defined as a jobject, it's not basic type of char.

    0 讨论(0)
提交回复
热议问题