JNI中新建文件、读写普通文件和驱动文件的方法综述fopen/open/creat/

来源:互联网 发布:游戏台式机推荐 知乎 编辑:程序博客网 时间:2024/05/05 07:42

这段时间的JNI开发中发现,在JNI中,普通文件的新建、读写既可以用C库函数,也可以用linux系统函数,如下:

平台android 4.4.2

编译工具ndk

static int write_file(void){int fd = 0;int ret = 0;char str[32];FILE *fp = NULL;//C库函数新建、写LOGI("fuck1.txt\n");fp = fopen("/storage/sdcard0/Download/fuck1.txt", "a");ERROR(fp == NULL, err1, "fuck1.txt create file");fprintf(fp, "%s\n", "fuck always!\n");fclose(fp);//linux系统函数open新建、写#if 1LOGI("fuck2.txt\n");fd = open("/storage/sdcard0/Download/fuck2.txt", O_CREAT | O_WRONLY, 0644);ERROR(fd < 0, err1, "fuck2.txt open file");strcpy(str, "hello world!\n");ret = write(fd, str, strlen(str));LOGI("2 write bytes =%d\n", ret);close(fd);#endif//linux系统函数creat/open 新建、写LOGI("fuck3.txt\n");fd = creat("/storage/sdcard0/Download/fuck3.txt", 0644);ERROR(fd < 0, err1, "fuck3.txt creat file");fd = open("/storage/sdcard0/Download/fuck3.txt", O_WRONLY);ERROR(fd < 0, err1, "fuck3.txt open file");strcpy(str, "fuck world!\n");ret = write(fd, str, strlen(str));LOGI("3 write bytes =%d\n", ret);close(fd);return 0;err1:return -1;}

在上面这个应用中,linux系统函数open的两个方法都可以成功使用:


<span style="white-space:pre"></span>int open(const char *pathname, int flags);       <span style="white-space:pre"></span>int open(const char *pathname, int flags, mode_t mode);

其中两个参数的open调用需要和creat函数结合使用。



但是在JNI中操作驱动设备文件时,则只能使用两个参数的open来打开,不能使用三个参数的open来打开!!!

因为我直接把ubuntu的程序(可以打开)移植到JNI中,一直open(三个参数的)失败,后来我改成了两个参数的open就可以打开。

这个问题先放着,后面再细究其原因!



JNI程序:

int V4l2::v4l2_open(struct video_information *vd_info){ERROR(NULL == vd_device, err1, "video device name NULL!");LOGI("video name = %s\n", vd_device);vd_info->fd = open(vd_device, O_RDWR);    if(vd_info->fd < 0)    {        LOGE("open: failed!");        return -1;    }    return 0;err1:return -1;}

































0 0
原创粉丝点击