C++ 在按下空格或制表符之前,如何从标准输入读取输入?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9054399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 12:14:40  来源:igfitidea点击:

How do I read input from standard input until a space or a tab is pressed?

c++

提问by AAaa

I need to read input from standard input until a space or a TAB is pressed.

我需要从标准输入读取输入,直到按下空格或 TAB。

Eventually, I need to hold the input in a std::stringobject.

最终,我需要将输入保存在一个std::string对象中。

采纳答案by Kerrek SB

Since a tab counts as whitespace, you are asking to extract the first token from the stream. This is very easy in C++, as token extraction is already the default behaviour of the >>operator:

由于选项卡计为whitespace,因此您要求从流中提取第一个标记。这在 C++ 中非常容易,因为标记提取已经是>>运算符的默认行为:

#include <iostream>
#include <string>

int main()
{
    std::string token;

    if (!(std::cin >> token)) { /* error */ return 1; }

    // now you have the first token in "token".
}

(The error can only occur in arcane circumstances, e.g. when the input file descriptor is already closed when the program starts.)

(错误只能发生在神秘的情况下,例如当程序启动时输入文件描述符已经关闭时。)

回答by Armen Tsirunyan

#include <iostream>
#include <string>

int main()
{
    std::string result;
    std::cin >> std::noskipws; //don't skip whitespaces
    char c;
    while (std::cin >> c)
    {
        if(c == '\t' || c == ' ')
             break;
        result.push_back(c);
    }

}

回答by Ilya Kogan

You can use getch()or getchar()to read each character separately, and then process the input manually so that when a space or a tab is pressed, you end the input.

您可以使用getch()getchar()分别读取每个字符,然后手动处理输入,以便在按下空格或制表符时结束输入。

回答by Dr G

This should do the job:

这应该可以完成这项工作:

#include <stdio.h>

int main ()
{
  char c;
  do {
    c=getchar();
    /** Your code here **/
  } while ((c != ' ') && (c != '\t'));
  return 0;
}

回答by nijansen

You can have std::cinto stop reading at any designated character using

您可以std::cin使用以下命令在任何指定字符处停止阅读

char temp[100];
std::cin.getline(temp, 100, '\t');

I don't know if you can easily get it to work with two characters though.

我不知道你是否可以轻松地让它与两个字符一起工作。

回答by James Kanze

The simple answer is that you can't. In fact, the way you've formulated the question shows that you don't understand istreaminput: there's no such thing as “pressed” in istream, because there's no guarantee that the input is coming from a keyboard.

简单的答案是你不能。事实上,你提出问题的方式表明你不理解istream输入:没有“按下”这样的东西istream,因为不能保证输入来自键盘。

What you probably need is cursesor ncurses, which does understand keyboard input; console output and the rest.

您可能需要的是cursesor ncurses,它可以理解键盘输入;控制台输出和其余的。

回答by Dietmar Kühl

It seems that the solution to this is, indeed, a lot easier using scanf("%[^\t ]", buffer). If you want to do it using C++ IOStreams I think the nicest to use option is to install a std::localewith a modified std::ctype<char>facet which uses a modified interpretation of what is considered to be a space and then read a std::string(see e.g. this answerI gave for a similar problem). Independent of whether you are using a C or a C++ approach you probably need to turn off line buffering on the standard input if you want to find out about the space or the tab when it entered rather than when the entire line is given to your program.

似乎解决这个问题的方法确实要容易得多,使用scanf("%[^\t ]", buffer). 如果你想使用 C++ IOStreams 来做到这一点,我认为最好的使用选项是安装一个std::locale带有修改过的std::ctype<char>方面,它使用对被认为是空格的修改解释,然后读取一个std::string(参见例如我给出的这个答案)类似的问题)。无论您使用的是 C 还是 C++ 方法,如果您想在输入时找出空格或制表符而不是将整行提供给程序时,您可能需要关闭标准输入上的行缓冲.

回答by Global Warrior

use scanf for this:

为此使用 scanf:

A simple code:

一个简单的代码:

#include<iostream>
#include<stdio.h>
int main()
{
        char t[1000]={'##代码##'};
        scanf("%[^\t ]",t);
        printf("%s",t);
        std::string mains(t);
        return 1;

}