coding-horror/basic-computer-games · warning
Could not create file!
Error message
Could not create file!
What it means
This panic is triggered by `.expect("Could not create file!")` on `file.write_all("".as_bytes())` in Splat's `Stats::new()` (line 45). Writing zero bytes is effectively a no-op for data purposes, so this `.expect` is really checking that the newly created file handle is writable. It would fail only on a very unusual post-create I/O error.
Source
Thrown at 81_Splat/rust/src/stats.rs:45
for s in stats {
if s.is_empty() {
continue;
}
let s = s.parse::<f32>().expect("Corrupt stats file!");
altitudes.push(s);
}
return Some(Stats { altitudes });
} else {
println!("PREVIOUS GAME DATA NOT FOUND!");
if !utility::prompt_bool("WOULD YOU LIKE TO CREATE ONE?", false) {
return None;
} else {
let mut file = File::create(path).expect("Invalid file path!");
file.write_all("".as_bytes())
.expect("Could not create file!");
return Some(Stats {
altitudes: Vec::new(),
});
}
}
}
println!("\nRESULTS OF THIS SESSION WILL NOT BE SAVED.");
None
}
pub fn add_altitude(&mut self, a: f32) {
let all_jumps = self.altitudes.len() + 1;
let mut placement = all_jumps;
for (i, altitude) in self.altitudes.iter().enumerate() {
if a <= *altitude {View on GitHub (pinned to 5301155192)
Solutions
- Remove the `write_all("")` call entirely — it writes nothing and is pointless after a successful `File::create`.
- If the intent is to truncate/initialize the file, use `write_all("\n".as_bytes())` or simply rely on `File::create` which already truncates.
- Replace `.expect` with error handling that reports the underlying `io::Error`.
Example fix
// before
let mut file = File::create(path).expect("Invalid file path!");
file.write_all("".as_bytes()).expect("Could not create file!");
// after
// File::create already truncates the file to empty,
// so the write_all("") call is unnecessary.
let mut file = match File::create(path) {
Ok(f) => f,
Err(e) => { eprintln!("Cannot create stats file: {}", e); return None; }
}; Defensive patterns
Strategy: try-catch
Validate before calling
// This write_all("") is a no-op; verify the file handle is writable
// by checking File::create succeeded (already done on the prior line).
// No additional validation is needed — remove the call. Type guard
// No type guard needed; the operation writes zero bytes. // The real guard is File::create's Result on the preceding line.
Try / catch
if let Err(e) = file.write_all("".as_bytes()) {
eprintln!("Warning: file write check failed: {}", e);
return None;
}
// Or better: remove the write_all("") call entirely. Prevention
- Remove no-op operations like write_all(empty_bytes) — they add failure points without benefit.
- File::create already truncates; no explicit empty write is needed.
- Handle file I/O errors at the operation that actually matters, not a redundant sanity check.
When it happens
Trigger: An I/O error occurs on a file handle that was just successfully created by `File::create` on the preceding line. This is rare in practice — the more likely failure path is line 43 (`File::create` itself). Possible if the filesystem experiences a transient error, the handle is invalid, or a concurrent process interferes.
Common situations: Disk full or quota exceeded between create and write. NFS or network filesystem latency/failure. Antivirus or security software locking the file between create and write. Extremely rare in local filesystems.
Related errors
- Corrupt stats file!
- Error loading stats data!
- ERROR WRITING Stats FILE!
- Invalid file path!
- Failed to read line.
AI-assisted analysis of coding-horror/basic-computer-games@5301155192 (2026-08-13).
Data as JSON: /api/errors/cb714f1e9edeb0e4.
Report an issue: GitHub.