c++ - 如何从控制台读取行并将其存储到 C++ 中的字符串中?

标签 c++ linux

我必须从控制台读取整行并将其存储到 std::stringchar 数组中, 例如

"Hi this is balaji"

现在我必须读取上面的字符串并将其存储到 string 中。我尝试使用 getline() 函数。

最佳答案

尝试:

#include <string>
#include <iostream>

int main()
{
    std::string line;

    std::getline(std::cin, line);  // read a line from std::cin into line

    std::cout << "Your Line Was (" << line << ")\n";

    std::getline(std::cin, line);  // Waits for the user to hit enter before closing the program
}

关于c++ - 如何从控制台读取行并将其存储到 C++ 中的字符串中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5759894/

相关文章:

c++ - 将映射中的对象(指针)输出到二进制文件

linux - 如何在 Linux 上查询 ATI 卡的显存大小?

linux - 在 unix 中将行转置为列

Gale-Shapley算法的C++实现

c++ - IR LLVM 形式与汇编

c++ - 如何同步终端上的输入和输出?

linux - 在 Unix 中使用时间戳重命名文件命令

linux - 关于 lz4mt 压缩和 linux 缓冲问题

linux - 在 bash 中执行双括号中的命令不起作用

c++ - Visual C++ 2008 中的默认项目