首页 > 解决方案 > 如何在此代码上撤消 cin 的重定向?

问题描述

我在下面的代码中使用了 dup2() ,现在我在让 cin 从屏幕读取时遇到问题:

#include <iostream>
#include <memory>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string>
#include <fstream>
#include <array>
#include <tuple>

using namespace std;

shared_ptr<string[]> getP_string(const char* cmd){
    unique_ptr<FILE, decltype(&pclose)> input(popen(cmd, "r"), pclose);
    int backInput = dup(0);
    dup2(fileno(input.get()),0);
    streambuf* stream_buffer_cin= cin.rdbuf();  
    if(!input){
        cerr << "won't open" << endl;
        exit(EXIT_FAILURE);
    }
    int count=0;
    int test;
    for (test=fgetc(stdin); test!=EOF; test=fgetc(stdin)){
        if (char(test) == '\n') count ++;
        cout << (char)test;
    }
    shared_ptr<string[]> results(new string[count]);
    cin.rdbuf(stream_buffer_cin); ///reading the cout above
    for(int i=0;i<count;i++){
        cin >> results[i];
    }
    close(fileno(input.get()));
    dup2(backInput,0);
    close(backInput);
    return results;
}

我使用的部分内容(第 15、33、34 和 35 行)来自这里:how to undo dup2

标签: c++iostreamfile-descriptor

解决方案


streambuf* stream_buffer_cin = cin.rdbuf();不保存流的状态,因此cin.rdbuf(stream_buffer_cin)不会倒带文件,只是将指针设置为相同的东西。而不是试图倒带文件,只需使用在std::string您插入时会增长的文件。

std::string getP_string(const char* cmd) {
  unique_ptr<FILE, decltype(&pclose)> input(popen(cmd, "r"), pclose);
  int backInput = dup(0);
  dup2(fileno(input.get()), 0);
  streambuf* stream_buffer_cin = cin.rdbuf();
  if (!input) {
    cerr << "won't open" << endl;
    exit(EXIT_FAILURE);
  }

  std::string results;
  std::copy(std::istreambuf_iterator<char>(std::cin), {},
            std::back_inserter(results));

  close(fileno(input.get()));
  dup2(backInput, 0);
  close(backInput);
  return results;
}

推荐阅读