Skip to content

file.eof() returns false, even if file is at EOF. #16239

@stianeikeland

Description

@stianeikeland

I open a file containing two bytes. Check EOF - false. Read two bytes, check EOF - false, read one byte - EOF error.

Is there a problem with File.eof() here, or am I doing something wrong?

use std::io::File;

// Create testfile using:
// echo -n -e '\x66\x6f' > testfile

fn main() {
    let mut fh = File::open(&Path::new("testfile")).unwrap();

    println!("{}", fh.eof()); // false
    println!("{}", fh.read_le_u16().unwrap()); // 28518
    println!("{}", fh.eof()); // false
    println!("{}", fh.read_u8().unwrap());
    // task '<main>' failed at 'called `Result::unwrap()`
    // on an `Err` value: couldn't read file (end of file; path=testfile)'
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions