读取标准输出文件



我创建了以下测试用例来模拟这个问题。我已经编译了源代码,可以模拟这个问题。

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;
}

最新更新