gpt4 book ai didi

file - 使用 BufReader 逐行读取文件,存储到数组中

转载 作者:行者123 更新时间:2023-11-29 08:14:38 27 4
gpt4 key购买 nike

在下面的代码中创建字符串缓冲区是我发现的最快方法,因为没有完成分配重新分配如果我没理解错的话

pub extern fn rust_print_file() -> *mut PackChar {

//set min size to 50 - avoid expanding when line count is 50 or less
let mut out_vec = Vec::with_capacity(50);
let mut curdr = env::current_dir().unwrap();//get path to file dir
let fl_str = "file_test.txt";
curdr.push(fl_str);//created full path to be used

let file = BufReader::new(File::open(curdr).unwrap());

//here i try to accommodate each line in a struct

let mut line_index = 0;
for line in file.lines() {

let cur_line = line.unwrap();

let loclbuf_size = cur_line.len();
let mut loclbuf = String::with_capacity(buffer_size);
//i tried two ways
loclbuf.push_str(cur_line.unwrap()); // can't be done
loclbuf.push_str(line.unwrap()); // can't be done too

let pack_char = PackChar {
int_val: line_index,
buffer_size: loclbuf_size as i32,
buffer: loclbuf.as_ptr() as *mut _,
};
line_index+=1;
mem::forget(buffer);
out_vec.push(pack_char);
}
Box::into_raw(out_vec.into_boxed_slice()) as *mut _
}

这是我用来传递给 C# 的结构

#[repr(C)]
pub struct PackChar {
pub int_val: c_int,
pub buffer: *mut c_char,
pub buffer_size: c_int,
}

当生成一些虚拟文本时我检查过,它正确地将数据传递给“另一端”以使用它。但不使用读取行任务,生成如上编码的文本。

这是我尝试过的另一种方式,虽然我更喜欢上面的代码,但是这个会引发编译错误。

错误:使用移动值:buffer [E0382] on forget(buffer)

#[no_mangle]
pub extern fn rust_return_file_read_lines() -> *mut PackChar {
let mut out_vec = Vec::with_capacity(50);

let mut curdr = env::current_dir().unwrap();
let fl_str = "file_test.txt";
curdr.push(fl_str);


let file = BufReader::new(File::open(curdr).unwrap());

let mut lindex = 0;
for line in file.lines() {

let tmpbuffer = line.unwrap().into_bytes();
let tmpbuffer_size = buffer.len();

let pack_char = PackChar {
int_val: lindex,
buffer_size: tmpbuffer_size as i32,
buffer: Box::into_raw(tmpbuffer.into_boxed_slice()) as *mut _
};
lindex+=1;
mem::forget(buffer);
out_vec.push(pack_char);

}
Box::into_raw(out_vec.into_boxed_slice()) as *mut _
}

编辑只要缓冲区的类型:

缓冲区:loclbuf.as_ptr() as *mut _,

我可以将数据正确传递给 C#。所以我怎样才能以这种方式读取行,以便每行都按照描述存储到缓冲区中?

最佳答案

现在看来,我的 visual studio 中似乎有一个错误,这不是第一次发生,但由于我是 rust 的新手,我确信代码是错误的。

这就是对我有用的,我很乐意提出意见和建议

extern crate libc;
use std::env;
use libc::c_char;
use libc::c_int;
use std::mem;
use std::io::{BufReader, BufRead};
use std::fs::File;

#[repr(C)]
pub struct PackChar {
pub int_val: c_int,
pub buffer: *mut c_char, // changed
pub dbuffer_size: c_int, // added
}


#[no_mangle]
pub extern fn rust_print_file() -> *mut PackChar {
let mut out_vec = Vec::with_capacity(50 as usize);

let mut cwd = env::current_dir().unwrap();
let fl_str = "file_test.txt";
cwd.push(fl_str);
let file = BufReader::new(File::open(cwd).unwrap());

for (index, line) in file.lines().enumerate() {

let buffer = line.unwrap();
let buffer_size = buffer.len();

let pack_char = PackChar {
int_val: index as i32,
dbuffer_size: buffer_size as i32,
buffer: buffer.as_ptr() as *mut _,
};
mem::forget(buffer); // don't deallocate memory
out_vec.push(pack_char);
}

Box::into_raw(out_vec.into_boxed_slice()) as *mut _ // changed
}

关于file - 使用 BufReader 逐行读取文件,存储到数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33595362/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com