你好,我想通过windows调用chcp 65001从我的程序中运行系统命令。
我的程序是用生锈写的,但使用外部c。
extern "C" {
pub fn system(command: *const u8);
}
fn main() {
unsafe {
system("\"\"chcp\" \"65001\"\"".as_ptr());
}
// do work
}调用可以工作,但我无法将字符串格式化以正确传递到system。
我尝试了下面的组合,下面是这问题。
"\"\"chcp\" \"65001\"\"" (锈蚀格式)
有了这个结果:Parameterformat wrong - "65001"
我还尝试了其他一些变体,但都没有成功:
"\"\"chcp\" 65001\""
结果:Parameterformat wrong - /rustc
"\"\"chcp 65001\""
结果the system can't find the given path
第一个变体seam很好,唯一的问题是chcp确实抱怨\"
你知道怎么通过吗?
奖励:为什么'/rustc‘出现在变体2中。
谢谢你的帮助!
发布于 2022-01-13 10:47:32
生锈字符串不是以NUL结尾的,所以您对系统的调用是在字符串之后读取内存中的任何内容。我想这就是你的/rustc的来源。您需要将字符串转换为CString,以确保其正确终止:
system (CString::new ("chcp 65001").unwrap().as_ptr());https://stackoverflow.com/questions/70694653
复制相似问题