不想把命令结果导入临时文件,然后读文件...,请问该怎么办?

例如 find . -name "A.txt"这条命令,查找到a.txt后会输出 “路径/a.txt”。
如果我在C++代码中用system("find . -name "A.txt"")执行了该条命令,那么如何用某个自定义的字符串取到 “路径/a.txt” 这段字符串呢?
system("find . -name "A.txt"");
string strFileName = ???

阿晨1998
浏览 191回答 2
2回答

慕沐林林

在windows下面这个的确是需要用管道来实现的VC6参考代码:#include <windows.h>#include <stdio.h>BOOL ExcudeCmd(char *szOutPutBuf,char *szCmdLine){SECURITY_ATTRIBUTES sa;&nbsp;HANDLE hRead,hWrite;sa.nLength = sizeof(SECURITY_ATTRIBUTES);&nbsp;sa.lpSecurityDescriptor = NULL;&nbsp;sa.bInheritHandle = TRUE; //输出重定向if (!CreatePipe(&hRead,&hWrite,&sa,0))&nbsp;{&nbsp;printf("创建匿名管道失败");return FALSE;&nbsp;}&nbsp;STARTUPINFO si;&nbsp;PROCESS_INFORMATION pi;&nbsp;ZeroMemory(&si,sizeof(STARTUPINFO));si.cb = sizeof(STARTUPINFO);&nbsp;si.hStdInput=hRead;si.hStdError = GetStdHandle(STD_ERROR_HANDLE); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//把创建进程的标准错误输出重定向到管道输入&nbsp;si.hStdOutput = hWrite; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //把创建进程的标准输出重定向到管道输入&nbsp;si.wShowWindow = SW_HIDE;si.dwFlags =STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;&nbsp;if (!CreateProcess(NULL, szCmdLine,NULL,NULL,TRUE,0,NULL,NULL,&si,&pi))&nbsp;{&nbsp;CloseHandle(hWrite);&nbsp;CloseHandle(hRead);&nbsp;printf("创建子进程失败");&nbsp;return FALSE;&nbsp;}&nbsp;else{CloseHandle(pi.hProcess);CloseHandle(pi.hThread);}DWORD bytesRead;&nbsp;if (!ReadFile(hRead,szOutPutBuf,1000,&bytesRead,NULL))&nbsp;{printf("读数据失败");&nbsp;return FALSE;}CloseHandle(hRead);&nbsp;return TRUE;}int main(){char cmdline[]="cmd.exe /c echo 回显的信息",buf[1000];ZeroMemory(buf,100);ExcudeCmd(buf,cmdline);printf(buf);//buf就是你想要的东西}Linux下面就不清楚了

守着一只汪

您可以试下类似的代码,popen 函数在 vc6 中对应的应该是 _popen ,pclose 为 _pclose或者百度 “c++ 获取 system 的输出” 您可以得到很多类似的问题的解决方案。#include&nbsp;<stdio.h>#include&nbsp;<string>&nbsp;void&nbsp;executeCMD(const&nbsp;char&nbsp;*cmd,&nbsp;char&nbsp;*result){&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;buf_ps[1024];&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;ps[1024]={0};&nbsp;&nbsp;&nbsp;&nbsp;FILE&nbsp;*ptr;&nbsp;&nbsp;&nbsp;&nbsp;strcpy(ps,&nbsp;cmd);&nbsp;&nbsp;&nbsp;&nbsp;if((ptr=popen(ps,&nbsp;"r"))!=NULL)&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while(fgets(buf_ps,&nbsp;1024,&nbsp;ptr)!=NULL)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strcat(result,&nbsp;buf_ps);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(strlen(result)>1024)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pclose(ptr);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ptr&nbsp;=&nbsp;NULL;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("popen&nbsp;%s&nbsp;error\n",&nbsp;ps);&nbsp;&nbsp;&nbsp;&nbsp;}}&nbsp;int&nbsp;main(){&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;result[1024];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;executeCMD("find&nbsp;.&nbsp;-name&nbsp;\"A.txt\"",&nbsp;result);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("%s",&nbsp;result&nbsp;);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;0;}&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP