Why won't my file io code work?

I've been trying to get a handle on working with file io. Specifically, reading data from a file and saving it in a vector. ( I've already received, here on this forum, some help on this topic.) However, the code below works sometimes and other times it doesn't. Here's a code snippet that works:

fn main() {
    let fname = "integers";
    let vector = read_file_to_vec(&fname);
    println!("\n The file contains {:?} \n", vector);
}

fn read_file_to_vec(fname: &str) -> Vec<i64> {
    let mut newvec: Vec<i64> = Vec::new();

    let file = File::open("integers").unwrap();
    let reader = BufReader::new(file);

    for line in reader.lines() {
        let line = line.unwrap();
        let num = line.parse().unwrap();
        newvec.push(num);
    }
    newvec
}

The integers file looks like this:

3
-65
45
-345

If I change the type to f64 it also works just fine. However if I change the type to u64 it throws the following error:

thread 'main' panicked at src/main.rs:48:32:
called `Result::unwrap()` on an `Err` value: ParseIntError { kind: InvalidDigit }

Note: The file for the unsigned values looks like this:

3
65
45
345

and the type in the read_file_to_vec() function is changed to u64 like this:

fn read_file_to_vec(fname: &str) -> Vec<u64> {
    let mut newvec: Vec<u64> = Vec::new();

and the line that causes the panic is this:

        let num = line.parse().unwrap();

Any ideas as to what I'm doing wrong? Thanks.

Are you sure you are parsing the right file? And if yes, are you certain your file looks like what you posted here? Could you maybe debug print line before trying to parse it and show us the output right before the panic happens?

2 Likes

Works for me. Are you sure you are reading the correct file? Maybe try printing out each line before parsing.

1 Like

lol we literally posted the exact same thing at the same time

1 Like

Also notice the warning from the compiler that the argument fname is never used. Did you possibly change the filename inside main but not inside read_file_to_vec

2 Likes

I've been wondering where that error came from. No, I changed it in main() and then it gets passed to the function. Not sure why it's telling me it is "unused". Doesn't make sense.

I just tried replacing the unsigned file completely, and still get the same error.

Next, I will try printing the line before parsing. Will let you know the result.

It's unused in read_file_to_vec, not main.

1 Like

Ok, well I found the problem. When I printed the lines before parsing, the values were clearly from the integers test file I created to test the i64 type. Yet in main() I had clearly changed the file name to unsigned with u64 values in it for testing. What I didn't change was the file name when opening the file. I had:

    let file = File::open("integers").unwrap();

instead of:

    let file = File::open(fname).unwrap();

Fixed it and everything works now. I figured it was going to turn out to be some simple, not-so-smart kind of mistake.

Next, I need to convert this to use generics. Will come back when (not if) I need help.

Thanks for helping out, guys! :nerd_face:

Glad to help. Rust really tries to give helpful error messages and I always encourage everyone to read them. It just takes a while of getting used to.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.