Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
364 views
in Technique[技术] by (71.8m points)

rust - how read mixed binary/text file with io::Cursor and byteorder?

I wanna read/parse a data file mixed binary/text. This file isn't utf_8 format and can't be converted.

The structure of the header of my data file is :

#[derive(Debug)]   
Struct DataParse{
       
       file_num : u16{2}, 
       file_name: char{16},
       file_size: u32{4},
       file_off: u32{4},
       file_dum: u8{1}
    
}

I use io::Cursor with the memory buffer and the crate byteorder to operate on my file :

use std::io;
use std::io::prelude::*;
use std::fs::File;
use std::path::Path;
use byteorder::{LittleEndian, ReadBytesExt};
use std::io::Cursor;

    fn read_parse_file(path_of_file: &str) -> io::Result<()> {
    
        let path = Path::new(&path_of_file);
        let display = path.display();
        println!("xxx : {}", &display);
    
        let mut f = File::open(&path)?;  
        let mut buffer = vec![0; 27];
    
        f.read_exact(&mut buffer).unwrap(); 
    
        let mut vec_pointer = Cursor::new(buffer);
    
        let filenum = vec_pointer.read_u16::<LittleEndian>().unwrap();
        let filename = // ?????????
        let filesize = vec_pointer.read_u32::<LittleEndian>().unwrap();
        let fileoff = vec_pointer.read_u32::<LittleEndian>().unwrap();
        let filendum = vec_pointer.read_u8().unwrap();
    
        let dat_binary = DataParse {file_num: filenum, file_name: filename, file_size: filesize, file_off: fileoff,  file_dum: filendum  };

        println!("{:?}", dat_binary);

    
        Ok(())
    
    }

My question is, how get "Struct file_name" of char type in a mixed binary/text in my case?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Thanks to @ddulaney for helping me.

fn read_parse_file(path_of_file: &str) -> io::Result<()> {

    let path = Path::new(&path_of_file);
    let display = path.display();
    println!("path is : {}", &display);

    let mut f = File::open(&path)?;

    let mut buffer = vec![0; 27];
    let mut string_buffer = vec![0u8; 16];

    f.read_exact(&mut buffer).unwrap(); 
  
    let mut vec_pointer = Cursor::new(buffer);

    let filenum = vec_pointer.read_u16::<LittleEndian>().unwrap();

    vec_pointer.read_exact(&mut string_buffer).unwrap();
    let mut filename = String::from_utf8(string_buffer).unwrap(); 
    match filename.find(''){
        None => {},
        Some(v) => { filename.truncate(v) },   
    }
        
    let filesize = vec_pointer.read_u32::<LittleEndian>().unwrap();
    let fileoff = vec_pointer.read_u32::<LittleEndian>().unwrap();
    let filendum = vec_pointer.read_u8().unwrap();

    let dat_binary = DatBinary {file_num : filenum, filename_of_ressource : filename, size : filesize, off : fileoff, dum : filendum}; 

    println!("{:?}", dat_binary);  

    Ok(())

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...