0
的Visual C++ 2017年 - 控制檯應用程序給出錯誤的命令時_perror返回非空
我找到一個Windows版本POPEN,即_popen唯一的文檔說,這應該是它失敗時返回一個NULL_PTR。它沒有提到向控制檯噴出無故障的錯誤消息。然而,如果返回非null和噴發。我還沒有找到確定管道是否連通的方法。當我使用fprintf寫入壞管道時,它看起來也不會失敗。這是_popen中的錯誤,還是我做錯了?什麼是解決方法?
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
int main()
{
// This program is expected to write
// 00000000
// Good
//
// Instead it writes (e.g.), in this order,
// 0082F2C0
// Not good
// '42' is not recognized as an interal or external command,
// operable program or batch file.
FILE* fp = _popen("42 is a no-good command", "w");
std::cout << fp << std::endl;
if (!fp) {
std::cout << "Good" << std::endl;
}
else {
std::cout << "Not good" << std::endl;
}
return 0;
}
popen()是一個Posix函數,保留在MSVC++ CRT中並帶有前導下劃線,因爲Windows曾經有一個Posix子系統。它使用Unix敏感性,將您指定的命令傳遞給命令行處理器Cmd.exe。如果它是垃圾,它會很高興地投入你的控制檯,它不會重定向stderr。這被認爲是25年前的一項功能,通過管道發送錯誤消息使得Bejeezus不知道什麼程序使用管道。現在它看起來像一個Windows程序員:)並沒有錯誤,cmd.exe開始就好了。 –
https://msdn.microsoft.com/en-us/library/windows/desktop/ms682499(v=vs.85).aspx –