[SOLVED] Multiple file reading

Hello. I want to read one file 3 times. I have a *.txt file with 3 matrix. Type matrix has method to read/write 1 matrix. In my function i try to call 3 times a function to write 3 different matrix in one file by appending next matrix in end of file.

pub fn save_to(&self, mut file: &File) {
    self.weights.save_to(&mut file);
    self.centres.save_to(&mut file);
    self.widths .save_to(&mut file);
}

and i get succes. Then i want to read this file and parse to 3 matrix. I use function below

pub fn load_from(mut file: &File) -> RBFN {
    let weights = Matrix::load_from(&mut file);
    let centres = Matrix::load_from(&mut file);
    let widths  = Matrix::load_from(&mut file);

    let architecture = (centres.rows(), widths.cols(), weights.rows());

    RBFN { architecture, weights, centres, widths }
}

But test panicked with error 'called Result::unwrap() on an Err value: ParseIntError { kind: Empty }'. Function save_to

    pub fn save_to(&self, mut file: &File) {
        writeln!(file, "{}", self.rows().to_string());
        writeln!(file, "{}", self.cols().to_string());

        for i in 0..self.rows() {
            for j in 0..self.cols() {
                writeln!(file, "{}", self[i][j].to_string());
            }
        }
    }

and load_from.

pub fn load_from(mut file: &File) -> Matrix {
    let mut f = BufReader::new(file);

    let mut buffer = String::new();

    f.read_line(&mut buffer).unwrap();
    let rows: usize = buffer.trim().parse::<usize>().unwrap();
    buffer.clear();

    f.read_line(&mut buffer).unwrap();
    let cols: usize = buffer.trim().parse::<usize>().unwrap();
    buffer.clear();

    let mut matrix = Matrix::zeros((rows, cols));

    for i in 0..matrix.rows() {
        for j in 0..matrix.cols() {
            f.read_line(&mut buffer).unwrap();
                    
            matrix[i][j] = buffer.trim().parse::<f64>().unwrap();
            buffer.clear();
        }
    }

    matrix
}

I don't know why &mut file not remember next data and say that i want to parse an empty string. Thanks for reading.

The BufReader will read more than just a line at a time from the underlying File. If it didn't, it would have to read only one byte at a time to avoid going too far, which sort of defeats the point of buffering.

It will probably work better if you create one shared BufReader, and pass that to your individual loads.

Thanks for answer! I try your last case and its work.