c++ - 如何在此代码上撤消 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
解决方案
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;
}
推荐阅读
- php - PHP根据2个键从数组中删除重复项和它们的总和
- excel - 每次工作表在 excel VBA 中受保护或不受保护时记录
- python - 如果使用 Python 的 Pandas 在两个日期之间的日期时间,寻找一种按日期时间分组的方法
- mongodb - 无法从函数中更新 Meteor 中的 Mongo Collection
- r - 大型生态数据集,metaMDS 不会收敛
- jquery - 在 JQuery 函数中更改 css 样式
- c# - 使用 EF Core dbcontext 运行异步选择时出错
- android - RequireNativeComponent:在 UIManager 中找不到“ToolbarAndroid”
- intune - 无法使用 intune 公司门户下载证书
- flutter - Flutter SingledChildScrollView > Stack > Positioned 是 Scroll 上的故障