rust - How to read a GBK-encoded file into a String? -
use std::env; use std::fs::file; use std::io::prelude::*; fn main() { let args: vec<string> = env::args().collect(); let filename = &args[1]; let mut f = file::open(filename).expect("file not found"); let mut contents = string::new(); f.read_to_string(&mut contents).expect("something went wrong reading file"); println!("file content:\n{}", contents); }
when attempt read gbk encoded file, following error:
thread 'main' panicked @ 'something went wrong reading file: error { repr: custom(custom { kind: invaliddata, error: stringerror("stream did not contain valid utf-8") }) }', /checkout/src/libcore/result.rs:860
it says stream must contain valid utf-8. how can read gbk file?
you want encoding
crate.
Comments
Post a Comment