相关疑难解决方法(0)

将字符串注入'cin'

我有一个从std :: cin读取用户输入的函数,我想编写一个将一些字符串插入std :: cin的unittest,这样以后从std :: cin中提取将读取该字符串而不是暂停键盘输入.

理想情况下,我会更改函数签名,以便我可以将自定义istream作为参数传递,但我不能这样做,因为我有一个我无法更改的固定界面.

cin.putback()几乎就是我想要的,但是它一次只插入一个字符,并且它以相反的顺序插入它们(但是我读到某个地方放回原本不存在的字符可能会有危险,尽管该网站没有详细说明原因).我已经尝试了几种方法将字符串注入cin的内部缓冲区cin.rdbuf(),但没有一种方法可以正常工作.我也考虑过使用外部测试脚本或创建子进程,但是我想首先考虑使用纯C++进行测试.

那么,是否有任何方法将字符串放入cin?或者你知道更好的方式来注入我的"假键盘输入"吗?

c++ iostream

12
推荐指数
2
解决办法
1万
查看次数

如何知道C++ 11上何时通过终端管道输入?

如何知道C++ 11上何时通过终端管道输入?

我可以这样称呼我的程序:

1. ./main solved.txt
2. cat unsolved.txt | ./main
3. cat unsolved.txt | ./main solved.txt
Run Code Online (Sandbox Code Playgroud)

我正在使用它来知道我是否需要在C POSIX标准上从管道读取数据:

#include <iostream>
#include <sstream>
#include <stdio.h>
#include <unistd.h>

int main( int argumentsCount, char* argumentsStringList[] )
{
    std::stringstream inputedPipeLineString;

    if( argumentsCount > 1 )
    {
        printf( "argumentsStringList[1]: %s", argumentsStringList[ 1 ] );
    }

    // If it is passed input through the terminal pipe line, get it.
    if( !isatty( fileno( stdin ) ) )
    {
        // Converts the std::fstream "std::cin" to std::stringstream which …
Run Code Online (Sandbox Code Playgroud)

c++ posix c++11

10
推荐指数
1
解决办法
489
查看次数

暂停功能永远循环

我正在尝试在C ++中实现暂停功能,但它会永远循环。

我正在使用macOS,但是我试图创建一个在任何系统中都可以使用的暂停功能...我相信我的cin >>不能从键盘上捕获'\ n'或'\ r',并且它永远循环。

void Transferencia::pause() {
    char enter = 0;
    while(enter != '\n' && enter != '\r') {
        cout << "(Press Enter to Continue...) ";
        cin >> enter;
    }
    cin.clear();
}
Run Code Online (Sandbox Code Playgroud)

我想暂停程序,直到用户按下“输入”键。但是,即使我按“输入/返回”,它仍会循环播放...

c++ pause

-1
推荐指数
1
解决办法
78
查看次数

标签 统计

c++ ×3

c++11 ×1

iostream ×1

pause ×1

posix ×1