读取标准输出文件
Read stdout file
我创建了以下测试用例来模拟这个问题。我已经编译了源代码,可以模拟这个问题。
1)当系统命令时,我们得到一些控制台输出(即您的作业提交),使用dup2重定向到文件并创建文件。stdout。
当我试图读取这个文件,因为我需要作业提交信息,我没有得到控制台输出的数据。我可以得到我写的数据。(确认文件操作).
我们不能从子进程创建的文件中读取控制台输出吗?
*change rundir and cmd
#include <string>
#include <vector>
#include <utility>
#include <sstream>
#include <fstream>
#include <iostream>
#include <iterator>
#include <algorithm>
#include <functional>
#include <map>
#include <sys/stat.h>
#include <fcntl.h>
using namespace std;
int child();
int main()
{
string rundir = "/temp";
pid_t id =child();
string _xafile;
string _afile;
string _afilecmd;
string line;
stringstream ss(stringstream::in | stringstream::out);
ss<<int(id);
_xafile = rundir;
_xafile = _xafile + "/" + ss.str()+".stdout";
cout<<" _xafile is "<<_xafile<<endl;
string cmd ="cat "+ _xafile + ">" + rundir + "/" + "process.txt";
_afile = rundir;
_afile = _afile + "/" + "process.txt";
_afilecmd = "rm -rf "+ _afile;
system(cmd.c_str());
ifstream xafile(_afile.c_str());
while(xafile)
{
string word;
xafile >> word;
cout<<word<<" ";
/* try to read console output but did not read data */
}
system(_afilecmd.c_str());
return 0;
}
int child()
{
string rundir = "/tmp";
string cmd = " tool <input file> -o <outputfile>";
const char* std_out_file;
pid_t pid = fork();
if (pid < 0) {
return -1;
}
if (pid == 0) {
pid_t mypid = getpid();
stringstream sm;
sm << rundir << "/";
if (strlen(std_out_file) == 0)
sm << mypid << ".stdout";
else
sm << std_out_file;
int fd = open(sm.str().c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644);
dup2(fd, 1);
dup2(fd, 2);
int fd2 = open("/dev/zero", O_RDONLY);
dup2(fd2, 0);
cout<<cmd <<endl<<endl;
// execl("/bin/sh", "/bin/sh", "-c", cmd, NULL);
system(cmd.c_str());
/*When system call it generate console output like : your job submitted */
/* redirect to file */
exit(-1);
}
if (pid > 0)
return pid;
cout<<" child is done"<<endl;
return 0;
}
这里不完全清楚您的想法-您的代码似乎派生了一个子程序,该子程序执行了一堆花哨的io操作,试图将应用程序的stdout重定向到一个文件,然后使用system()运行命令。代码中的命令有自己的重定向,特别是通过"-o",所以它可能不会写入标准输出。
返回到父进程中,您尝试打开用于读取的与子进程打开用于写入的文件相同的文件。你没有同步,所以它们可以以任何顺序发生。您似乎试图使用"cat"来读取文件并试图读取cat的标准输出?
我认为你实际上试图做的是Perl的
的C/c++等价物$foo = `ls -l /tmp`;
或类似的东西—执行命令并捕获输出。
更好的方法是使用管道。
#include <iostream>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
enum { WritePipe = 1, ReadPipe = 0 };
int main(int argc, const char** argv)
{
int pipes[2]; // each process is going to have its own file descriptor.
if(pipe(pipes) != 0) {
perror("pipe failed");
return -1;
}
pid_t pid = fork();
if(pid == 0) {
// child
close(pipes[ReadPipe]); // close the parent pipe in our context.
// redirect stdout and stderr to the pipe.
dup2(pipes[WritePipe], STDOUT_FILENO);
dup2(pipes[WritePipe], STDERR_FILENO);
// close(STDERR_FILENO); // <- or just do this to close stderr.
int result = system("ls -ltr /etc");
close(pipes[WritePipe]);
return result;
}
if(pid < 0) {
perror("fork failed");
return -1;
}
// Parent process has launched the child.
close(pipes[WritePipe]);
char buffer[4097];
int bytesRead;
while((bytesRead = read(pipes[ReadPipe], buffer, sizeof(buffer) - 1)) > 0) {
buffer[bytesRead] = 0;
std::cout << buffer;
}
std::cout << std::endl;
close(pipes[ReadPipe]);
return 0;
}
相关文章:
- 捕获标准输出以压缩并使用 CTRL-C 中断会给出损坏的 zip 文件
- 从文件读取并通过矢量输出到控制台
- 将文本文件读取到2D数组中,但数组开始为超过数组[39][]的任何内容输出胡言乱语
- 如何读取和输出文件的内容及其包含的字数?
- Win32 GUI C(++) 应用将标准输出和标准输出重定向到磁盘上的同一文件
- 使用 getline 从输入文件读取一行到输出文件
- 使用 QProcess 读取标准输出
- C++ 输出在读取文本文件时跳过最后一个单词
- 将文本文件读取到双向链表并将其输出 - C++
- 是否可以在重定向之前读取标准输出
- 无法在 cpp 文件中通过可视化标准输出构建 yuvlib 库函数
- 读取 Qt 中连续 QProcess 的标准输出
- Windows Batch - 如何在将标准输出重定向到文件时添加时间戳
- 从.txt文件读取输入输出C++
- 从文件读取后,输出中的额外零行
- 如何使用BOOST_LOG_TRIVIAL宏将任何内容记录到标准输出/标准和文件
- 将文件的全部内容读取到标准输出的最短方法是什么
- 从txt文件中获取char并在标准输出C++上进行编写
- QProcess -> 从 Linux 上的 sftp 读取标准输出
- 读取标准输出文件