如何在android中使用NDK读取c中的文件

时间:2017-05-14 12:24:56

标签: android c android-ndk

我正在尝试使用NDK读取c中的文件。当我调试代码时,我看到我在f(文件读取指针)中得到一个NULL指针。另外,我需要读取的文件位于cpp下的同一文件夹中,包括所有其他头文件。我在我的应用程序中拥有外部存储的读写权限。任何帮助表示赞赏。

C代码:



#ifndef client_h
#define client_h

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <pthread.h>
#include "utility.h"


#define SENDBUFSIZE 1399
#define RECVBUFSIZE 1399

void pingServer(char *hostname, int portno)
{
  //Some other functionality which works correctly


  // File Read operations
  FILE *f = fopen("somefile.csv", "r"); //Get the null pointer here
  if (f == NULL)
    perror("Could not open input file");
  char *currentRow[3];
  char *nextRow[3];
  int rows = 3;

  for (int i=0; i<rows; i++)
  {
    currentRow[i] = (char*)malloc(20 * sizeof(char));
    nextRow[i] = (char*)malloc(20 * sizeof(char));
  }
  double current, next;
  double elapsed;
  int isNextRow, isCurentRow;
  readCsvLine(f,currentRow,rows); //Get the headers
  int counter = 2;
  isCurentRow = readCsvLine(f,currentRow,rows);

  //sends the data

  //Free the memory
  for (int i=0; i<rows; i++)
  {
    free(currentRow[i]);
    free(nextRow[i]);
  }
  free(buf);
  return;

}

char* printName(){
    char* ptr = "hello From C";
    return ptr;
}

#endif
&#13;
&#13;
&#13;

用于调用c方法的Java代码:

&#13;
&#13;
#include <jni.h>
#include <string>
#include "client.h"

extern "C"
JNIEXPORT jstring JNICALL
Java_com_example_habbas_mobiledifferentiation_MainActivity_stringFromJNI(
        JNIEnv *env,
        jobject /* this */) {
    char *hostname = "127.0.0.1";
    int portno = 1254;
    pingServer(hostname, portno);
    std::string hello = printName();
    return env->NewStringUTF(hello.c_str());
}
&#13;
&#13;
&#13;

0 个答案:

没有答案