array(2) { ["docs"]=> array(0) { } ["count"]=> int(0) } 111string(0) "" int(1) int(10) int(70) int(8640000) string(13) "likecs_art_db" array(1) { ["query"]=> array(1) { ["match_all"]=> object(stdClass)#28 (0) { } } } array(1) { ["createtime.keyword"]=> array(1) { ["order"]=> string(4) "desc" } } int(10) int(0) int(8640000) array(2) { ["docs"]=> array(0) { } ["count"]=> int(0) } android内核读取file文件 - 爱码网

内核读取file文件的方法:

char* file_read(const char * file_path) 
{
    struct file *file = NULL; //保存打开文件的文件指针变量
    struct inode *inode = NULL; //为了获取文件大小用的inode结构变量
    char *file_buf; //保存开辟的内存空间的地址的指针变量
    loff_t fsize; //保存文件大小的变量
    mm_segment_t old_fs; //保存内存边界的变量
    static char error[] = "none"; 
    file = filp_open(file_path,O_RDWR,0664);//打开文件
    
    if (IS_ERR(file)) 
      {return error;
      }
    inode = file->f_dentry->d_inode;//获取文件的大小
    fsize = inode->i_size;
    old_fs = get_fs();
    set_fs(KERNEL_DS);
    
    loff_t *pos = &(file->f_pos);
    file_buf = (char *)kmalloc(fsize+1,GFP_KERNEL);
    
    vfs_read(file, file_buf, fsize, pos); //读操作
    
    filp_close(file, NULL); //关闭文件
    
    set_fs(old_fs);//边界恢复
    
    return file_buf;
}

字符串转int的方法:

int string_to_int(const char *str){
    int result = 0;
    int signal = 1;
    if(((*str>='0')&&(*str<='9'))||(*str=='-')||(*str=='+')){
        if((*str=='-')||(*str=='+')){
            if((*str=='-')){
                signal = -1;
                str++;
            }
        }
    }else{
        return 65;
    }
    while((*str>='0')&&(*str<='9')){
        result = result*10 + (*str++ - '0');
    }
    return signal*result;
}

 

相关文章: