file - 如何使用带有 BufReader 的文件并且仍然能够写入它?

标签 file rust ownership

我想打开一个文件并使用 lines() 作为 BufReader 读取其内容。我还希望能够查找到文件末尾并写入一些新行。

使用 let mut file 让我写入文件,但是一旦我将文件提供给 BufReader 我就不能再写入了,因为主要函数不再拥有 file:

fn main() {
    let filename = "tt.txt";

    // open a tt.txt file in the local directory
    let file = OpenOptions::new()
        .read(true)
        .write(true)
        .create(true)
        .open(filename)
        .unwrap();

    // now read the whole file to get the latest state
    let date_re = Regex::new(r"^(\d{4})-(\d{2})-(\d{2})").unwrap();
    let time_activity_re = Regex::new(r"^(\d{2}):(\d{2})\s*(.*)").unwrap();
    let reader = BufReader::new(file);
    let mut latest_date: Option<Date<Local>> = None;
    let mut latest_datetime: Option<DateTime<Local>> = None;
    let mut latest_activity: Option<String> = None;

    for wrapped_line in reader.lines() {
        let line = wrapped_line.unwrap();
        println!("line: {}", line);

        if date_re.is_match(&line) {
            let captures = date_re.captures(&line).unwrap();
            let year = captures.at(1).unwrap().parse::<i32>().unwrap();
            let month = captures.at(2).unwrap().parse::<u32>().unwrap();
            let day = captures.at(3).unwrap().parse::<u32>().unwrap();
            latest_date = Some(Local.ymd(year, month, day));
            latest_datetime = None;
            latest_activity = None;
        }

        if time_activity_re.is_match(&line) && latest_date != None {
            let captures = time_activity_re.captures(&line).unwrap();
            let hour = captures.at(1).unwrap().parse::<u32>().unwrap();
            let minute = captures.at(2).unwrap().parse::<u32>().unwrap();
            let activity = captures.at(3).unwrap();

            latest_datetime = Some(latest_date.unwrap().and_hms(hour, minute, 0));

            latest_activity = if activity.len() > 0 {
                // TODO: if latest_activity already constains a string, clear it and reuse it
                // as per: https://stackoverflow.com/questions/33781625/how-to-allocate-a-string-before-you-know-how-big-it-needs-to-be
                Some(activity.to_string())
            } else {
                None
            };

            println!("time activity: {} |{}|", latest_datetime.unwrap(), activity);
        }
    }

    // FIXME: I have to open a second file descriptor to the same file, in order to be able to write to it
    let mut out = OpenOptions::new()
        .read(true)
        .write(true)
        .create(true)
        .open(filename)
        .unwrap();

    out.seek(End(0));

    let now = Local::now();
    if latest_date == None || latest_date.unwrap().year() != now.year()
        || latest_date.unwrap().month() != now.month()
        || latest_date.unwrap().day() != now.day()
    {
        if (latest_date != None) {
            // not an empy file, as far as tt is concerned
            out.write_all(b"\n\n");
        }
        out.write_all(format!("{}\n", now.format("%Y-%m-%d")).as_bytes());
        out.write_all(b"\n");
    }

    let activity = env::args().skip(1).join(" ");
    if (activity.len() > 0) {
        out.write_all(format!("{} {}\n", now.format("%H:%M"), activity).as_bytes());
    } else {
        // if there was no latest activity *and* there is no activity, then there's no point in writing a second blank line with just a time
        if latest_activity == None {
            return;
        }
        out.write_all(format!("{}\n", now.format("%H:%M")).as_bytes());
    }

    // FIXME: we're just relying on the program exit to close the two file descriptors (which point at the same file).
}

如何使用单个文件描述符读取现有行并追加新行?

(代码来自https://github.com/chrisdew/tt/blob/e899f252014391f2e01c3cc9e281cab1ab88936f/src/main.rs)

最佳答案

为避免移动值,您可以使用引用和新范围。 以下是如何做到这一点:

fn main() {
    let filename = "tt.txt";

    // open a tt.txt file in the local directory
    let mut file = OpenOptions::new()
        .read(true)
        .write(true)
        .create(true)
        .open(filename)
        .unwrap();

    // now read the whole file to get the latest state
    let date_re = Regex::new(r"^(\d{4})-(\d{2})-(\d{2})").unwrap();
    let time_activity_re = Regex::new(r"^(\d{2}):(\d{2})\s*(.*)").unwrap();
    {
        // BufReader now borrows the value instead of taking ownership.
        let reader = BufReader::new(&mut file);
        let mut latest_date: Option<Date<Local>> = None;
        let mut latest_datetime: Option<DateTime<Local>> = None;
        let mut latest_activity: Option<String> = None;

        for wrapped_line in reader.lines() {
            let line = wrapped_line.unwrap();
            println!("line: {}", line);

            if date_re.is_match(&line) {
                let captures = date_re.captures(&line).unwrap();
                let year = captures.at(1).unwrap().parse::<i32>().unwrap();
                let month = captures.at(2).unwrap().parse::<u32>().unwrap();
                let day = captures.at(3).unwrap().parse::<u32>().unwrap();
                latest_date = Some(Local.ymd(year, month, day));
                latest_datetime = None;
                latest_activity = None;
            }

            if time_activity_re.is_match(&line) && latest_date != None {
                let captures = time_activity_re.captures(&line).unwrap();
                let hour = captures.at(1).unwrap().parse::<u32>().unwrap();
                let minute = captures.at(2).unwrap().parse::<u32>().unwrap();
                let activity = captures.at(3).unwrap();

                latest_datetime = Some(latest_date.unwrap().and_hms(hour, minute, 0));

                latest_activity = if activity.len() > 0 {
                    // TODO: if latest_activity already constains a string, clear it and reuse it
                    // as per: https://stackoverflow.com/questions/33781625/how-to-allocate-a-string-before-you-know-how-big-it-needs-to-be
                    Some(activity.to_string())
                } else {
                    None
                };

                println!("time activity: {} |{}|", latest_datetime.unwrap(), activity);
            }
        }
    }
    // End of the scope, so now file is not borrowed anymore.

    file.seek(End(0));

    let now = Local::now();
    if latest_date == None || latest_date.unwrap().year() != now.year()
        || latest_date.unwrap().month() != now.month()
        || latest_date.unwrap().day() != now.day()
    {
        if (latest_date != None) {
            // not an empy file, as far as tt is concerned
            file.write_all(b"\n\n");
        }
        file.write_all(format!("{}\n", now.format("%Y-%m-%d")).as_bytes());
        file.write_all(b"\n");
    }

    let activity = env::args().skip(1).join(" ");
    if (activity.len() > 0) {
        file.write_all(format!("{} {}\n", now.format("%H:%M"), activity).as_bytes());
    } else {
        // if there was no latest activity *and* there is no activity, then there's no point in writing a second blank line with just a time
        if latest_activity == None {
            return;
        }
        file.write_all(format!("{}\n", now.format("%H:%M")).as_bytes());
    }

    // FIXME: we're just relying on the program exit to close the two file descriptors (which point at the same file).
}

关于file - 如何使用带有 BufReader 的文件并且仍然能够写入它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33831265/

相关文章:

c++ - 采用预创建值的所有者对象?错误的设计?

file - 使用 Netbeans 时,在哪里将 txt 文件添加为 Java Web 应用程序中的资源?

rust - 如何为具有 "rented"引用的类型实现特征

rust - 为什么基于 len() 索引一个可变向量被认为是同时借用?

rust - 为 Rust 编写 C 库绑定(bind)的目的是什么?

c++ - 如何将 shared_ptr 传递给 naked ptr 函数

rust - 枚举值的向量会产生所有权问题

android - 在 Parse.com 获取图像文件的链接

java - 无法重命名文件

Android:永久保存文件(即使在清除数据/卸载后)