如何将system()函数的输出重定向到变量
How do I redirect the output of a system() function to a variable?
我有这个代码:
system("echo %username%");
我想将它的结果重定向到一个变量,比如uname
。
我该怎么做?
我知道WinAPI,但我想这样做。
快速、丑陋和肮脏的方法是将输出重定向到一个文件,然后读取该文件。
system("echo %username% > someFile.txt");
更详细的方法是通过以下命令行使用CreateProcess
API:cmd.exe /c echo %username%
API允许您指定自定义标准输入和标准输出。您可以为标准输出创建一个管道,如下所示:
HANDLE g_hChildStd_OUT_Wr = NULL;
SECURITY_ATTRIBUTES saAttr;
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE;
saAttr.lpSecurityDescriptor = NULL;
// Create a pipe for the child process's STDOUT.
//
if ( !CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &saAttr, 0) ) return -1;
然后在CreateProcess API中使用此管道。类似这样的东西:
TCHAR szCmdline[]=TEXT("cmd.exe /c echo %username%");
PROCESS_INFORMATION piProcInfo;
STARTUPINFO siStartInfo;
// Set up members of the PROCESS_INFORMATION structure.
//
memset( &piProcInfo, 0, sizeof(PROCESS_INFORMATION) );
// Set up members of the STARTUPINFO structure.
// This structure specifies the STDIN and STDOUT handles for redirection.
//
memset( &siStartInfo, 0, sizeof(STARTUPINFO) );
siStartInfo.cb = sizeof(STARTUPINFO);
siStartInfo.hStdOutput = g_hChildStd_OUT_Wr;
siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
// Create the child process.
//
bSuccess = CreateProcess(NULL,
szCmdline, // command line
NULL, // process security attributes
NULL, // primary thread security attributes
TRUE, // handles are inherited
0, // creation flags
NULL, // use parent's environment
NULL, // use parent's current directory
&siStartInfo, // STARTUPINFO pointer
&piProcInfo); // receives PROCESS_INFORMATION
然后从管道中读取类似的内容:
DWORD dwRead, dwWritten;
CHAR chBuf[BUFSIZE];
BOOL bSuccess = FALSE;
HANDLE hParentStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
for (;;)
{
bSuccess = ReadFile( g_hChildStd_OUT_Rd, chBuf, BUFSIZE, &dwRead, NULL);
if( ! bSuccess || dwRead == 0 ) break;
}
进程将异步运行,因此您需要知道进程何时终止并进行正确的清理。因此,为了使这项工作发挥作用,这里有很多细节需要学习。
一个完整的例子可以在这里找到:http://msdn.microsoft.com/en-us/library/windows/desktop/ms682499(v=vs.85).aspx
如果目标只是获取用户名,您是否考虑过getenv
;getenv( "username" )
将直接返回。
否则,如果你有更多的事情要做,并且希望结果在一个文件中。。。传递给system
的字符串将传递给命令解释器,因此您可以在cmd.exe
中的命令行中执行的任何操作都将在system
中起作用:文件重定向、管道到另一个进程等。
相关文章:
- 将标准输出重定向到ostream
- 在程序执行期间从标准输出重定向到自定义流
- 将 Tcl 中的命令输出重定向到文件时,文件以 DOS 格式生成
- 将 Linux system() 调用命令的输出重定向到仅可变的
- 将 ffmpeg 控制台输出重定向到C++中的字符串或文件
- Win32 GUI C(++) 应用将标准输出和标准输出重定向到磁盘上的同一文件
- 将线程之间的标准输入/输出重定向
- GDB Cygwin 不会将输出重定向到文件
- 如何将 printf 流输出重定向到.csv文件
- 将PowerShell.exe的输入和输出重定向到C 中的管道
- Windows Batch - 如何在将标准输出重定向到文件时添加时间戳
- 将一个进程标准输出重定向到管道将产生不同的运行结果
- 如何将我的应用程序生成的标准 Mac OS X 日志输出重定向到文件
- 正在将控制台输出重定向到同一解决方案中的MFC屏幕输出
- 函数 system() 中的输出重定向似乎不起作用
- 使用 ofstream 将控制台输出重定向到文件C++末尾
- 将 printf C++控制台输出重定向到 C#
- 将打印到控制台的函数输出重定向到字符串
- 为外壳输出重定向创建文件
- 将调试输出重定向到空流而不是 std::cerr