read customer data

This commit is contained in:
2025-04-21 20:08:46 +08:00
parent 7f869e6bf8
commit cd1d8981bb
2 changed files with 39 additions and 1 deletions

View File

@@ -10,8 +10,43 @@ struct CmdArgs {
file: String, file: String,
} }
#[derive(Debug)]
struct Customer {
id: u32,
arrival_time: u32,
service_time: u32,
}
fn read_customers_from_file(path: &Path) -> Vec<Customer> {
let file = File::open(path).expect(&format!("Open file {} failed.", path.to_str().expect("Invalid path")));
let reader = io::BufReader::new(file);
let mut customers = Vec::new();
for line in reader.lines() {
let line = line.expect("Read lien failed");
let parts: Vec<&str> = line.split_whitespace().collect();
if parts.len() != 3 {
continue; // or handle the error
}
let customer = Customer {
id: parts[0].parse().expect("Invalid ID"),
arrival_time: parts[1].parse().expect("Invalid arrival time"),
service_time: parts[2].parse().expect("Invalid service time"),
};
customers.push(customer);
}
customers
}
fn main() { fn main() {
let args = CmdArgs::parse(); let args = CmdArgs::parse();
println!("File name: {}", args.file); let customers = read_customers_from_file(Path::new(&args.file));
for cus in &customers {
println!("{:?}", cus);
}
} }

3
lab1/test_data/1.txt Normal file
View File

@@ -0,0 +1,3 @@
1 1 10
2 5 2
3 6 3