C++;(预C+;+;11)由sstream初始化的fstream 我试图打开其他进程STDIN,并用C++、DUP()和其他C技巧编写它。但不幸的是,采用字符串的fstream构造函数似乎只能从C++11中获得(源代码:)

C++;(预C+;+;11)由sstream初始化的fstream 我试图打开其他进程STDIN,并用C++、DUP()和其他C技巧编写它。但不幸的是,采用字符串的fstream构造函数似乎只能从C++11中获得(源代码:),c++,stringstream,C++,Stringstream,C++;(预C+;+;11)由sstream初始化的fstream 我试图打开其他进程STDIN,并用C++、DUP()和其他C技巧编写它。但不幸的是,采用字符串的fstream构造函数似乎只能从C++11中获得(源代码:) #包括 #包括 #包括 //使用名称空间std; int main() { pid_t pid=fork(); 如果(pid==0) { //std::cout在C++11中总是存在std::ofstream other(pidstr.str

C++;(预C+;+;11)由sstream初始化的fstream 我试图打开其他进程STDIN,并用C++、DUP()和其他C技巧编写它。但不幸的是,采用字符串的fstream构造函数似乎只能从C++11中获得(源代码:)

#包括
#包括
#包括
//使用名称空间std;
int main()
{
pid_t pid=fork();
如果(pid==0)
{

//std::cout在C++11中总是存在
std::ofstream other(pidstr.str());
(尽管您无论如何都应该使用
std::ofstream
)。我尝试过这种方法,但include给了我错误。我认为这很奇怪,但无论如何最好包含的库不超过(?)1.是的;2.有更好的方法,但我不能加入到这个评论中;3.甚至不要使用
sync()
,它的行为是由实现定义的,很可能对您没有任何帮助;4.我认为这可能是一个bug。但我应该更仔细地查看您的代码以确定。关于(2):这不需要跨平台,需要将数据传递到其他pid标准。其中更好的方法是什么?3:我读到我实际上应该使用sync或clear,至少在某个地方:)令人困惑,但如果getline确定要等待一段时间,我就不会使用它(不管子级是否在父级发送输入之前开始执行并进入while循环)@user41632,它位于
中,就像您使用的
std::ifstream
一样。
#include <iostream>
#include <fstream>
#include <sstream>

//using namespace std;

int main()
{
    pid_t pid = fork();
    if (pid == 0)
    {
        // std::cout << "child:" << i << std::endl;
        std::string line;
        std::cin.sync();
        std::cout << "child got message:" << std::endl;
        while ( std::getline(std::cin, line) )
        {
        std::cout << line << std::endl;

        }
        std::cout << "child done receiving" << std::endl;


    }
    else{
        //std::cout << "parent"<<std::endl;
        std::ifstream file("stdin", std::ios::in);
        if(file.is_open())
        {
        std::string tmp, str;

        std::stringstream pidstr;
        pidstr << "/proc/" << pid << "/fd/0";

        while ( std::getline(file, tmp) )
            str += tmp + "\n";

        std::fstream other( (pidstr.str().c_str()), std::ios::out);
        other << str ;
        }
    }

    return 0;
}
    while ( std::getline(std::cin, line) )
    {
    std::cout << line << std::endl;
    }
    std::cout << "child done" << std::endl;
    std::getline(std::cin, line);
    int lines; // this is single line sent as first from parent, containing int
    std::istringstream toint(line);
    toint >> lines;
    std::cout << "how much lines should it read:" << lines << std::endl;